-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
29 lines (23 loc) · 793 Bytes
/
index.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
// Node server which will handle socket io connections
const io = require('socket.io')(8000, {
cors: {
origin: '*',
}
});
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', message => {
socket.broadcast.emit('left', users[socket.id]);
delete users[socket.id];
});
})