-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (29 loc) · 1.32 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
import {Server} from 'socket.io';
import express from 'express';
import { createServer } from 'http';
import Connection from './database/db.js';
import { getDocument, updateDocument } from './controller/document-controller.js'
const PORT =process.env.PORT || 9000;
const URL = process.env.MONGODB_URI || `mongodb://atul786:test@cluster0-shard-00-00.baisg.mongodb.net:27017,cluster0-shard-00-01.baisg.mongodb.net:27017,cluster0-shard-00-02.baisg.mongodb.net:27017/google-docs?ssl=true&replicaSet=atlas-6leq5p-shard-0&authSource=admin&retryWrites=true&w=majority`;
Connection(URL);
const app=express();
if(process.env.NODE_ENV==='production'){
app.use(express.static('client/build'));
//app.use(express.static(path.join(__dirname,"client","build")))
}
const httpServer=createServer(app);
httpServer.listen(PORT);
const io=new Server(httpServer);
io.on('connection', socket => {
socket.on('get-document', async documentId => {
const document = await getDocument(documentId);
socket.join(documentId);
socket.emit('load-document', document.data);
socket.on('send-changes', delta => {
socket.broadcast.to(documentId).emit('receive-changes', delta);
})
socket.on('save-document', async data => {
await updateDocument(documentId,data);
})
})
});