-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (43 loc) · 1.61 KB
/
index.js
File metadata and controls
50 lines (43 loc) · 1.61 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
//requiring necessary modules for the backend
const express = require("express");
const socketio = require("socket.io");
const http = require("http");
const router = require("./router.js")
const { addUser, removeUser, getUser, getUsersInRoom } = require("./userFunction");
const port = 5000;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(router);
//waiting for connection from a particular socket
io.on("connection", (socket) => {
console.log("user has joined");
//waiting for joining connection and in callback function adding the user to array
socket.on("join",({name, room}, callback ) => {
var id = socket.id;
const data = addUser({id, name, room});
if(data.error){
return callback && callback(data);
}
//admin generated messages...
socket.emit('message',{user: 'admin', text: `Hey ${data.name}, welcome to the ${data.room} :) `});
socket.broadcast.to(data.room).emit('message',{ user: 'admin', text: `${data.name} has joined ${data.room}`});
socket.join(data.room);
callback && callback(data);
});
// waiting for send messages from particular socket
socket.on('sendMessage', (message, callback) => {
const user = getUser(socket.id);
//sending this message from particular socket to all other users in the same room
io.to(user.room).emit('message', {user: user.name, text: message});
callback();
});
//disconnecting socket
socket.on("disconnect", (callback) => {
console.log("user left");
const user = removeUser(socket.id);
});
});
server.listen(port,() => {
console.log("server has started");
})