-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (60 loc) · 1.86 KB
/
app.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
import express from "express";
import { createServer } from "http";
import { Server } from "socket.io";
import dotenv from "dotenv";
dotenv.config({
path: "./.env",
});
import { connectDB } from "./connection.js";
import { Document } from "./model.js";
const PORT = process.env.PORT || 3000;
connectDB()
.then(() => console.log("MongoDB Connected"))
.catch((err) => console.log(err));
const findOrCreateDocument = async (id) => {
if (!id) return;
const document = await Document.findById(id);
if (document) {
document.users++;
return await document.save();
};
return await Document.create({ _id: id, data: "", users: 1 });
};
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: ["https://frontend-collaborative-text-editor.vercel.app", "http://localhost:5173"],
methods: ["GET", "POST"],
},
});
io.on("connection", (socket) => {
const doc_id = socket.handshake.query.id;
socket.on("get-document", async (id) => {
const document = await findOrCreateDocument(id);
socket.join(id);
// socket.emit("total-user", document.users);
socket.broadcast.to(id).emit("total-user", document.users);
socket.emit("load-document", {data: document.data, users: document.users});
socket.on("send-changes", (data) => {
socket.broadcast.to(id).emit("receive-changes", data);
});
socket.on("save-changes", async (data) => {
// console.log(data);
await Document.findByIdAndUpdate(id, { data: data });
});
});
socket.on("disconnect", async () => {
const id = doc_id;
const doc = await Document.findById(id);
doc.users--;
await doc.save();
socket.broadcast.to(id).emit("total-user", doc.users);
});
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
httpServer.listen(PORT, () => {
console.log(`listening on *:${PORT}`);
});