-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
46 lines (39 loc) · 1.19 KB
/
utils.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
const connection = (client) => {
// event fired when the chat room is disconnected
// client.on("disconnect", () => {
// users = users.filter((user) => user.socketId !== client.id);
// });
// add identity of user mapped to the socket id
global.users.push({
socketId: client.id,
userId: "userId",
});
client.on("identity", (userId) => {
global.users.push({
socketId: client.id,
userId: userId,
});
});
// subscribe person to chat & other user as well
client.on("subscribe", (room, otherUserId = "") => {
this.subscribeOtherUser(room, otherUserId);
client.join(room);
});
// mute a chat room
client.on("unsubscribe", (room) => {
client.leave(room);
});
console.log("Here is user",global.users)
}
const subscribeOtherUser = (room, otherUserId) => {
const userSockets = users.filter(
(user) => user.userId === otherUserId
);
userSockets.map((userInfo) => {
const socketConn = global.io.sockets.connected(userInfo.socketId);
if (socketConn) {
socketConn.join(room);
}
});
}
module.exports = { connection, subscribeOtherUser};