-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-io.js
78 lines (68 loc) · 2.37 KB
/
socket-io.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
const { Server } = require('socket.io');
const server = require('./server');
const SOCKET_EVENTS = require('./constants/socket-events');
const USER_MAPPING = {};
const io = new Server(server, {
cors: { origin: process.env.FRONTEND_URL, methods: ['GET', 'POST'] },
});
const getConnectedClients = (roomId) =>
Array.from(io.sockets.adapter.rooms.get(roomId) || []).map((socketId) => ({
socketId,
client: USER_MAPPING[socketId],
}));
io.on('connection', (socket) => {
socket.on(SOCKET_EVENTS.JOIN, ({ roomId, user }) => {
USER_MAPPING[socket.id] = user;
// GETTING ALL THE CLIENTS THAT ARE CONNECTED TO THE ROOM by its "roomId"
const clients = getConnectedClients(roomId);
// NOW LOOP OVER THE CLIENTS ADD EMIT AN EVENT THAT A NEW CLIENT WANTS TO JOIN
// ALSO EMIT AN EVENT THE CURRENT CLIENT WHO WANTS TO JOIN PASSING SOME INFO
clients.forEach((connectedClient) => {
// FOR OTHER CLIENTS
io.to(connectedClient.socketId).emit(SOCKET_EVENTS.ADD_PEER, {
peerId: socket.id,
createOffer: false,
remoteUser: user,
});
// FOR THE CURRENT CLIENT
socket.emit(SOCKET_EVENTS.ADD_PEER, {
peerId: connectedClient.socketId,
createOffer: true,
remoteUser: USER_MAPPING[connectedClient.socketId],
});
});
socket.join(roomId);
});
// HANDLE ICECANDIDATE
socket.on(SOCKET_EVENTS.RELAY_ICE, ({ peerId, icecandidate }) =>
io.to(peerId).emit(SOCKET_EVENTS.ICE_CANDIDATE, {
peerId: socket.id,
icecandidate,
})
);
// HANDLE SDP (SESSION DESCRIPTION)
socket.on(SOCKET_EVENTS.RELAY_SDP, ({ peerId, sessionDescription }) =>
io.to(peerId).emit(SOCKET_EVENTS.SESSION_DESCRIPTION, {
peerId: socket.id,
sessionDescription,
})
);
// HANDLE REMOVE PEER
socket.on(SOCKET_EVENTS.LEAVE, leaveRoom);
socket.on('disconnecting', leaveRoom);
function leaveRoom({ roomId }) {
const connectedClients = getConnectedClients(roomId);
connectedClients.forEach((connectedClient) => {
io.to(connectedClient.socketId).emit(SOCKET_EVENTS.REMOVE_PEER, {
peerId: socket.id,
userId: USER_MAPPING[socket.id]?.id,
});
socket.emit(SOCKET_EVENTS.REMOVE_PEER, {
peerId: connectedClient.socketId,
userId: connectedClient.client?.id,
});
});
delete USER_MAPPING[socket.id];
socket.leave(roomId);
}
});