-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketHandlers.js
43 lines (34 loc) · 1.46 KB
/
socketHandlers.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
const { db } = require("./firebase")
module.exports = (socket) => {
console.log("Socket connected: " + socket.id)
// start listening for new users requesting to join the room
socket.on("join-room", async ({ username, userId, collabId, roomId }) => {
console.log("User [" + username + ":"+socket.id+"] added to room [" + roomId + "]")
socket.on('disconnect', () => {
socket.to(roomId).emit("user-disconnected", { username, userId })
console.log("User [" + username + ":"+socket.id+"] disconnected")
})
db.doc("collabs/" + collabId).get().then(snap => {
const collab = snap.data()
console.log(collabId)
socket.emit("load-document", { content: collab.content, published: collab.published })
})
// add them to the room
socket.join(roomId)
// broadcast to the whole room that this user joined
socket.to(roomId).emit("user-joined", socket.id, { userId, username })
})
// start listening for user data sent by that user to a new user
socket.on('user-joined ack', (socketid, data) => {
console.log('USER JOINED ACK: ' + data)
socket.to(socketid).emit("join-room ack", data)
})
socket.on("send-changes", (delta, { roomId }) => {
console.log('TEXT EDITOR CHANGES - SEND CHANGES: ' + delta, " - ROOM ID: " + roomId)
socket.to(roomId).emit("receive-changes", delta)
})
socket.on("send_message", (data) => {
console.log('TEXT EDITOR CHANGES - SEND MESSAGE: ' + data)
socket.to(data.roomId).emit("receive_message", data);
});
}