-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
154 lines (139 loc) · 3.27 KB
/
server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
.
.
========= Import and setup dependencies ============
.
.
*/
// core modules
const path = require('path');
const http = require('http');
// express initializaion
const express = require('express');
const app = express();
// import and initialize socket
const socketio = require('socket.io');
const server = http.createServer(app);
const io = socketio(server);
// global constants
const PORT = process.env.PORT || 3000;
const botName = 'JustTalk bot';
// utility functions
const formatMessage = require('./utils/messages');
const { userJoin, getCurrentUser, userLeaves, getRoomUsers } = require('./utils/users');
// serve static assets
app.use(express.static(path.join(__dirname, 'public')));
/*
.
.
========= 1. Client 'CONNECTION' Event (connected to port) ============
RECEIVED
.
.
*/
io.on('connection', (socket) => {
/*
.
.
2. 'JOINROOM' Event (userName and roomName received from frontend)
RECEIVED
.
.
*/
socket.on('Join Room', ({ username, room }) => {
/*
.
.
3. Push user in array (temporary db) with custom userJoin function
.
.
*/
const user = userJoin(socket.id, username, room);
/*
.
.
4. Join user to paritular room (as selected by user in index page)
.
.
*/
socket.join(user.room);
/*
.
.
5. Greet current user under 'MESSAGE' event with custom formatMessage function
SENT
.
.
*/
socket.emit(
'message',
formatMessage(
botName,
'Welcome to JustTalk! This is a chatting platform developed by Nikhil Sourav!'
)
);
/*
.
.
5. Notify everyone (except current user) that a new user is connected. Under 'MESSAGE' event
SENT
.
.
*/
socket.broadcast
.to(user.room)
.emit('message', formatMessage(botName, `${user.username} has joined the chat`));
/*
.
.
6. Send current room and list of all users in this particular room under 'ROOMUSERS' event
SENT
.
.
*/
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room),
});
/*
.
.
6. Receive message typed by user under 'CHATMESSAGE' event (msg is string)
RECEIVED
7. Send this message under 'MESSAGE' event to broadcast to everyone in this room
SEND
.
.
*/
socket.on('chatMessage', (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
/*
.
.
8. If connection by user is broken (user leaves), delete this user from db
9. Send 'USER LEFT' message under 'MESSAGE' event to broadcast to everyone in this room
.
.
*/
socket.on('disconnect', () => {
// delete user from temporary db
const user = userLeaves(socket.id);
// Send message that user left
if (user) {
io.to(user.room).emit(
'message',
formatMessage(botName, `${user.username} has left the chat`)
);
// Send users and room info to update
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room),
});
}
});
});
});
// fire up server
server.listen(PORT, () => console.log(`server up on port ${PORT}`));