-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
27 lines (22 loc) · 729 Bytes
/
Copy pathindex.js
File metadata and controls
27 lines (22 loc) · 729 Bytes
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
// node server which will handle socket.io connection
const io = require("socket.io")(8000);
const users = {};
io.on("connection", (socket) => {
// If any new user joins, let other users connected to the server know!
socket.on("new-user-joined", (name) => {
users[socket.id] = name;
socket.broadcast.emit("user-joined", name);
});
// If someone sends a message, broadcast it to other people
socket.on("send", (message) => {
socket.broadcast.emit("receive", {
message: message,
name: users[socket.id],
});
});
// If someone leaves the chat, let others know
socket.on("disconnect", () => {
socket.broadcast.emit("left", users[socket.id]);
delete users[socket.id];
});
});