-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (38 loc) · 1.89 KB
/
server.js
File metadata and controls
55 lines (38 loc) · 1.89 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
52
53
54
55
const path=require('path')
const http=require('http')
const express=require('express')
//Handling socket.io
const socketio=require('socket.io')
const app=express()
const server=http.createServer(app)
//initialize variable for socketio
const io=socketio(server)
const userInfo=require('./public/js/user')
const {userJoin,currentUser,LeaveUser,getRoomUsers}=require('./public/js/users')
//Setting static folder of html,css and js files
app.use(express.static(path.join(__dirname, 'public')))
//Run's when user joins
io.on('connection',socket=>{
socket.on('joinroom',({username,room})=>{
const user=userJoin(socket.id,username,room)
socket.join(user.room)
socket.emit('message',userInfo('Server','Welcome to the chat'))//server emmits message with message event to the client
//Broadcast when new user joins
socket.broadcast.to(user.room).emit('message',userInfo('Server',`${user.username} has joined the chat`))//Sends msg to every user except the client when he is connected
io.to(user.room).emit('userList',{room:user.room,users:getRoomUsers(user.room)})//List of users
})
//If user disconnects
socket.on('disconnect',()=>{
const user=LeaveUser(socket.id)
if(user){
io.to(user.room).emit('message',userInfo('Server',`${user.username} has left the chat`))//Sends msg to every user who is left
io.to(user.room).emit('userList',{room:user.room,users:getRoomUsers(user.room)})//List of users
}
})
//Listening for the event from clinet, msg is received in msg
socket.on('chatMsg',msg=>{
const getUser=currentUser(socket.id)//Returns the user object
io.to(getUser.room).emit('message',userInfo(getUser.username,msg))//Emmits the message to all the users connected so use io
})
})
server.listen(process.env.PORT || 3000,()=>console.log('Server started at port 3000',__dirname))