-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (34 loc) · 1.11 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
// express instance app
const app = require('express')();
// load static file
const serveStatic = require('serve-static');
// create http server
const http = require('http').createServer(app);
// create socket.io instance
const io = require('socket.io')(http);
// server listen port
const port = process.env.PORT || 4000;
app.use(serveStatic(__dirname + "/dist"));
// listen on connection
io.on('connection', async (socket) => {
//Listing when user join the chat chanel
socket.on('joinChat', (data) => {
socket.broadcast.emit('joinChat', (data));
socket.broadcast.emit('updateOnlineUsers');
});
//Listing when user send message
socket.on('sendMessage', data => {
socket.broadcast.emit('sendMessage', (data));
});
//Listing when user is typing
socket.on('typingNotification', data => {
socket.broadcast.emit('typingNotification', (data));
});
//Listing when user leave the chat chanel
socket.on('unJoinChat', data => {
socket.broadcast.emit('unJoinChat', (data));
});
});
http.listen(port, () => {
console.log(`Server started on port ${port}`);
});