-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (49 loc) · 1.88 KB
/
server.js
File metadata and controls
51 lines (49 loc) · 1.88 KB
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
const path = require('path');
const http = require('http')
const express = require('express');
const socketio = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const formatMessage = require('./utils/messages')
const {userJoin, getCurrentUser,userLeave,getRoomUsers} = require('./utils/users')
//set static folder
app.use(express.static(path.join(__dirname,'/public')));
const PORT = process.env.PORT || 3000;
const bot_name = "chitti Bot"
io.on('connection', socket => {
//get user and room info
socket.on('joinRoom', ({username, room })=>{
const user = userJoin(socket.id,username,room)
socket.join(user.room);
//when new user join the rom
socket.emit('message', formatMessage(bot_name, 'Welcome to chat-App'));
//when a user joins chat
socket.broadcast.to(user.room).emit('message', formatMessage(bot_name, `${user.username} joined the chat`));
//send users list
io.to(user.room).emit('roomUser',{
room : user.room,
users : getRoomUsers(user.room)
})
})
//when a user disconnects
socket.on('disconnect', () => {
const user = userLeave(socket.id)
if(user){
io.to(user.room).emit('message',formatMessage(bot_name, `${user.username} left the chat`));
//send users list
io.to(user.room).emit('roomUser',{
room : user.room,
users : getRoomUsers(user.room)
})
}
})
//listening for chat message
socket.on('chatMsg',(msg)=>{
const user = getCurrentUser(socket.id)
io.to(user.room).emit('message', formatMessage(user.username,msg))
})
})
server.listen(PORT,()=>{
console.log(`server runnning on https://localhost:${PORT}`);
})