-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (40 loc) · 1.17 KB
/
server.js
File metadata and controls
49 lines (40 loc) · 1.17 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
import dotenv from 'dotenv'
import http from 'http'
import { Server } from 'socket.io'
import app from './app.js'
dotenv.config()
const server = http.createServer(app)
const io = new Server(server, {
cors: {
origin: '*'
}
})
const port = process.env.PORT
// a map in place of a database to store message history
const msg_history = {}
io.on('connection', (socket) => {
console.log('A user connected')
socket.on('join_room', (room) => {
socket.join(room)
console.log(`User has joined room ${room}`)
io.to(room).emit('receive_message_history', msg_history)
})
socket.on('leave_room', (room) => {
socket.leave(room)
console.log(`User has left room ${room}`)
})
socket.on('disconnect', () => {
console.log('User disconnected')
})
socket.on('send_message', ({ room, message, username, time }) => {
// Add the message to the room's message history
if (!msg_history[room]) {
msg_history[room] = []
}
msg_history[room].push({message: message, username: username, time: time})
io.to(room).emit('receive_message_history', msg_history)
})
})
server.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})