-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (32 loc) · 1.23 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
const express = require('express'); // require karo express ko
const app = express() // app me express ki functionality dalo.
const server = require('http').Server(app); // app ko server me dalo.
const {v4: uuidv4} = require('uuid'); // for uniq ID
const io = require('socket.io')(server);
const {ExpressPeerServer} = require('peer')
const peerServer = ExpressPeerServer(server, {
debug: true
})
app.set('view engine', 'ejs'); // setting engine
app.use(express.static('public')); // IMP to include public files.
app.use('/peerjs', peerServer);
app.get('/', (req,res)=>{
res.redirect(`/${uuidv4()}`);
}) // create uniq id in URL
app.get('/:room', (req,res)=>{
res.render('room', {roomId: req.params.room})
})
//establishing real-time connection
io.on('connection', socket => {
socket.on('join-room', (roomId, userId)=>{
socket.join(roomId)
socket.to(roomId).emit('user-connected', userId)// tellin certain user has connected to everyone
socket.on('message', message=>{ // recieve msg
io.to(roomId).emit('createMessage', message) //sending msg to same room
})
socket.on('disconnect', () => {
socket.to(roomId).emit('user-disconnected', userId)
})
})
})
server.listen(3030);// server ko listen karo