-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsocket.js
38 lines (30 loc) · 991 Bytes
/
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
const { User } = require("./models/user");
const socket = socket => {
socket.on("new-user", async user => {
console.log("new-user: ", user.username);
socket.broadcast.emit("user-connected", user);
});
socket.on("disconnect", async () => {
const user = await User.findOneAndUpdate(
{ status: socket.id },
{ status: "" },
{ useFindAndModify: false }
);
console.log("user-disconnect: ", user.username);
socket.broadcast.emit("user-disconnected", user);
});
socket.on("user-offline", async () => {
const user = await User.findOneAndUpdate(
{ status: socket.id },
{ status: "" },
{ useFindAndModify: false }
);
console.log("user-disconnect: ", user.username);
socket.broadcast.emit("user-disconnected", user);
});
socket.on("broadcast-message", async msgObj => {
// console.log("broadcast-message: ", msgObj);
socket.broadcast.emit("new-message", msgObj);
});
};
module.exports = socket;