-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (51 loc) · 1.67 KB
/
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
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
// importing for basic express server
let express = require('express');
let app = express();
let http = require('http').Server(app);
// importing for io socket
let io = require('socket.io')(http);
// default route for server
app.use(express.static(path.join(__dirname, 'public')));
// we will use this route to send message
app.get('/send-message', (req, res) => {
socket.emit
});
let totalUsers = 0;
// listening to the socket for connect event.
io.on('connection', (socket) => {
let addUser = false;
// listening to addition of users on server
socket.on('add user', (username) => {
if(addUser) return;
// storing username in the socket session
socket.username = username;
++totalUsers;
addUser = true;
// this informs the socket that new user is added
socket.emit('login', {
numUsers: totalUsers,
});
// broadcast to all people that user has been added
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: totalUsers
});
});
// listening to messages on server
socket.on('new message', (data) => {
// this new message event returns data as parameter which stores the details of message
// you can find this in documentation
socket.broadcast.emit('new message', {
username: socket.username,
message: data,
});
});
// listening to disconnect event on the socket
socket.on('disconnect', () => {
console.log('A user is disconnected');
});
});
// starting the server on port 3000
http.listen(3000, () => {
console.log('server started...');
});