-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
163 lines (134 loc) · 4.8 KB
/
app.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
console.log(`Environment "${process.env.NODE_ENV}"`);
const express = require("express");
const Cors = require("cors");
const cookieParser = require("cookie-parser");
const helmet = require("helmet");
const router = require("./routes");
const connectDB = require("./config/database-config");
const AppError = require("./utils/AppError");
const errorHandler = require("./controllers/error-controller");
const ACTIONS = require("./actions");
const path = require("path");
const app = express();
/* CONFIGURE SOCKET.IO */
const server = require("http").createServer(app);
const io = require("socket.io")(server, {
cors: {
origin: [process.env.CLIENT_URL, "http://localhost:3000"],
methods: ["GET", "POST"],
},
});
/* CONNECT TO DATABASE */
connectDB();
/* MIDDLEWARES */
app.use(cookieParser());
app.use(helmet());
app.use(
Cors({
credentials: true,
origin: [process.env.CLIENT_URL, "http://localhost:3000"],
})
);
app.use(express.urlencoded({ extended: true }));
app.use(express.json({ limit: "5mb" }));
app.use("/storage", express.static("storage")); // for serving static files from server
/* ROUTES */
app.use("/api", router);
app.get("/api", (req, res) => {
res.status(200).json({
status: "success",
});
});
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "/public/index.html"));
});
app.all("*", (req, res, next) => {
const err = new AppError("resource not found", 404);
next(err);
});
app.use(errorHandler); // custom error handler
/* SOCKET CONNECTIONS */
const socketUserMapping = {};
io.on("connection", (socket) => {
socket.on(ACTIONS.JOIN, ({ roomId, user }) => {
socketUserMapping[socket.id] = user;
// get all the clients in a room with {roomId}
const clients = Array.from(io.sockets.adapter.rooms.get(roomId) || []);
clients.forEach((clientId) => {
// emit "add-peer" event to every client in a room (say to everyone "hey please add me in your connection")
io.to(clientId).emit(ACTIONS.ADD_PEER, {
peerSocketId: socket.id,
createOffer: false,
user: user,
});
socket.emit(ACTIONS.ADD_PEER, {
peerSocketId: clientId,
createOffer: true,
user: socketUserMapping[clientId],
});
});
socket.join(roomId);
});
// handle relay-ice
socket.on(ACTIONS.RELAY_ICE, ({ peerSocketId, icecandidate }) => {
io.to(peerSocketId).emit(ACTIONS.ICE_CANDIDATE, {
peerSocketId: socket.id,
icecandidate,
});
});
// handle relay-sdp (session description)
socket.on(ACTIONS.RELAY_SDP, ({ peerSocketId, sessionDescription }) => {
io.to(peerSocketId).emit(ACTIONS.SESSION_DESCRIPTION, {
peerSocketId: socket.id,
sessionDescription,
});
});
// handle mute
socket.on(ACTIONS.MUTE, ({ roomId, userId }) => {
const clients = Array.from(io.sockets.adapter.rooms.get(roomId) || []);
clients.forEach((clientId) => {
io.to(clientId).emit(ACTIONS.MUTE, {
peerSocketId: socket.id,
userId,
});
});
});
// handle unmute
socket.on(ACTIONS.UNMUTE, ({ roomId, userId }) => {
const clients = Array.from(io.sockets.adapter.rooms.get(roomId) || []);
clients.forEach((clientId) => {
io.to(clientId).emit(ACTIONS.UNMUTE, {
peerSocketId: socket.id,
userId,
});
});
});
// handle leaving the room
function handleLeaveRoom() {
const { rooms } = socket;
Array.from(rooms).forEach((roomId) => {
const clients = Array.from(
io.sockets.adapter.rooms.get(roomId) || []
);
clients.forEach((clientId) => {
// say to everyone "hey please remove me from your connection"
io.to(clientId).emit(ACTIONS.REMOVE_PEER, {
peerSocketId: socket.id,
userId: socketUserMapping[socket.id],
});
socket.emit(ACTIONS.REMOVE_PEER, {
peerSocketId: clientId,
userId: socketUserMapping[clientId],
});
});
socket.leave(roomId);
});
delete socketUserMapping[socket.id];
}
socket.on(ACTIONS.LEAVE, handleLeaveRoom);
socket.on("disconnecting", handleLeaveRoom); // when the browser is closed
});
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});