-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
56 lines (50 loc) · 1.42 KB
/
server.js
File metadata and controls
56 lines (50 loc) · 1.42 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
50
51
52
53
54
55
56
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const Notification = require('./models/Notification');
dotenv.config({
path: './config.env',
});
const app = require('./app');
mongoose
.connect(process.env.DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => {
console.log('Db connected');
})
.catch((e) => {
console.log(e, 'Failed to connect Db');
});
const server = app.listen(process.env.PORT || 3001, () => {
console.log('Server started');
});
const io = require('socket.io')(server, { pingTimeout: 60000 });
io.on('connection', (socket) => {
console.log('connected');
socket.on('authenticated', (userId) => {
console.log(userId, 'auth');
socket.join(userId);
});
socket.on('join room', (groupId) => {
socket.join(groupId);
});
socket.on('message', ({ groupId, message }) => {
socket.broadcast.to(groupId).emit('message', { groupId, message });
});
socket.on('seen', (groupId) => {
socket.broadcast.to(groupId).emit('seen', groupId);
});
socket.on('render', (groupId) => {
socket.broadcast.to(groupId).emit('render', groupId);
});
socket.on('notification', async (msg) => {
const notification = await Notification.create(msg);
socket.broadcast.to(msg.to).emit('notification');
});
socket.on('disconnect', () => {
console.log('disconnected');
});
});