-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
75 lines (59 loc) · 1.98 KB
/
socket.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
let users = []; //users refers to both agents & customers
//for chat b/w agents & customers
const addUser = (userId, socketId) => {
!users.some((user) => user.userId === userId) &&
users.push({ userId, socketId });
};
const removeUser = (socketId) => {
users = users.filter((user) => user.socketId !== socketId);
};
const getUser = (userId) => {
return users.find((user) => user.userId === userId);
};
//for updating Non alloted customers queries to all agent's side
const getAgents = () => {
return users.filter((user) => {
if(user.userId != null){
return user.userId.includes("A");
}
});
};
module.exports = function(io) {
io.on("connection", function(socket) {
//when connect
// console.log("a user connected.");
//take userId and socketId from user
socket.on("addUser", (userId) => {
console.log(userId, socket.id)
addUser(userId, socket.id);
io.emit("getUsers", users);
});
//send and get message
socket.on("sendMessage", ({ senderId, receiverId, text }) => {
const user = getUser(receiverId);
console.log(user)
if(user){
console.log(senderId, user)
io.to(user.socketId).emit("getMessage", {
senderId,
text,
});
}
});
//when disconnect
socket.on("disconnect", () => {
// console.log("a user disconnected!");
removeUser(socket.id);
io.emit("getUsers", users);
});
//for New created query by customer
socket.on("newQuery", (obj) => {
const agents = getAgents();
console.log(agents, obj)
agents.forEach((user) => {
console.log(user, obj)
io.to(user.socketId).emit("getNewQuery", obj);
});
});
});
};