-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
22 lines (19 loc) · 747 Bytes
/
index.js
File metadata and controls
22 lines (19 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//node server which will handle socket io connections
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 send a message then broadcast it to other people
socket.on('send',message=>{
socket.broadcast.emit('recieve', {message:message, name:users[socket.id]})
});
//if someone leaves the chat, let other know
socket.on('disconnect', message =>{
socket.broadcast.emit('right', user[socket.id]);
delete users[socket.id];
});
})