-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (39 loc) 路 1.64 KB
/
Copy pathserver.js
File metadata and controls
50 lines (39 loc) 路 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* --------------------------------------------------- */
/* DEPLOYING EXPRESS JS */
/* --------------------------------------------------- */
const express = require("express");
const app = express();
const server = require("http").Server(app);
const { v4: uuidv4 } = require("uuid"); //for unique room URL and ID
const io = require("socket.io")(server); //utilizing web sockets to deploy real time communications
/* --------------------------------------------------- */
/* PEER SERVER */
/* --------------------------------------------------- */
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, {
debug: true,
});
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use("/peerjs", peerServer);
//redirects the user to the required Room_ID
app.get("/", (req, rsp) => {
rsp.redirect(`/${uuidv4()}`);
});
app.get("/:room", (req, res) => {
res.render("room", { roomId: req.params.room });
});
/* --------------------------------------------------- */
/* CONNECTION OF PARTICIPANTS */
/* --------------------------------------------------- */
io.on("connection", (socket) => {
socket.on("join-room", (roomId, userId) => {
socket.join(roomId);
socket.to(roomId).broadcast.emit("user-connected", userId);
socket.on("message", (message) => {
io.to(roomId).emit("createMessage", message); //creates a message that a participant has joined the room
});
});
});
//port services
server.listen(process.env.PORT || 3030);