-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
79 lines (55 loc) · 2.17 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
const express = require("express")
const path=require("path");
const http = require("http");
const socketio = require("socket.io")
const formatMessage= require("./utils/messages")
const {getRoomUsers,userLeave,getCurUser,userJoin}= require("./utils/users")
//work socketio a server is must. that's why we created http server.
// createserver(Request_handler)....app function works as request listener function here.
const app=express()
const server= http.createServer(app);
//set static folder
app.use(express.static(path.join(__dirname,"public")))
//create websocket for server
const io=socketio(server);
const chatBot ="ChatCord Bot";
//run whenever client connects
io.on('connection',socket=>{
socket.on("joinRoom",({username,room})=>{
const user= userJoin(socket.id, username,room)
socket.join(user.room);
// broadcast when user connects to the user
socket.emit("message",formatMessage(chatBot,`Welcome to the ${user.room} room`));
// broadcast when user connects to all other users
socket.broadcast.to(user.room).emit("message",formatMessage(chatBot,`${user.username} has entered the chat`));
//
io.to(user.room).emit("roomUsers",{
room:user.room,
users:getRoomUsers(user.room)
})
})
// listen chat-message from client
socket.on("chat-message",(msg)=>{
const user = getCurUser(socket.id);
io.to(user.room).emit("message",formatMessage(user.username,msg));
})
// send when user leave the chat
socket.on("disconnect",()=>{
const user = userLeave(socket.id);
if(user){
io.to(user.room).emit("message",formatMessage(chatBot,`${user.username} has left the chat`));
// get all users in the room
io.to(user.room).emit("roomUsers",{
room:user.room,
usersList:getRoomUsers(user.room)
})
}
})
})
const Port = process.env.PORT || 5000;
server.listen(Port,()=>{
console.log("server started");
})
// io.emit() is used to send to all
// socket.broadcast.emit() is used to send to all except the one who send it.
// socket.emit() to send msg to particular client.