-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
73 lines (59 loc) · 2.24 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
const express = require('express')
const app = express()
const server = require('http').Server(app)
const io = require('socket.io')(server)
const { v4: uuidV4 } = require('uuid')
const { ExpressPeerServer } = require('peer')
const peerServer = ExpressPeerServer(server, {
debug: true,
})
const port = process.env.PORT || 3000
// to generate random roomId, using uuid
// this may need to live on the client side so that it can generate a link to then join the room
const roomId = uuidV4()
const chatRoomId = uuidV4()
app.set('view engine', 'ejs')
app.use(express.static('public'))
app.use('/peerjs', peerServer)
// on connection we go to the home page
app.get('/', (req, res) => {
// if you have a homepage for the application, you can use this:
res.render('index')
// to create a brand new room and redirect user there, we do this:
// res.redirect(`/room/${roomId}`)
})
// time to take roomID link from index.ejs and redirect to room.ejs when user clicks on room
app.get('/room/:room', (req, res) => {
// you get room from :room (which comes from link)
res.render('room', { roomId: req.params.room })
})
// need to open the room.ejs file and add the roomId to the roomId variable
// then add the roomId to the roomId variable in the room.ejs file
app.get('/room', (req, res) => {
res.redirect(`/room/${roomId}`)
})
// for the group chat, we already have id and name from Moralis DB
// i want to push the chatRoomId from front end/DB and render chatRoom.ejs
app.get('/chatRoom/:chatRoomId', (req, res) => {
res.render('chatRoom.ejs', { chatRoomId: req.params.chatRoomId })
})
// now we need to redirect to the chatRoom.ejs file
app.get('/chatRoom', (req, res) => {
res.redirect(`/chatRoom/${chatRoomId}`)
})
io.on('connection', (socket) => {
socket.on('join-room', (roomId, userId) => {
socket.join(roomId)
socket.broadcast.to(roomId).emit('user-connected', userId)
socket.on('message', (message, userId) => {
//send message to the same room
io.to(roomId).emit('createMessage', message, userId)
console.log('message created', message, userId)
})
socket.on('disconnect', () => {
socket.broadcast.to(roomId).emit('user-disconnected', userId)
})
})
})
server.listen(port)
console.log(`listening on port ${port}`)