-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.js
43 lines (41 loc) · 1.38 KB
/
socket.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 socketIo = require('socket.io');
const Message = require('./models/messageModel');
const Conversation = require('./models/conversationModel');
module.exports = (server) => {
const io = socketIo(server);
io.on('connection', (socket) => {
socket.emit('connected');
console.log('User connected', socket.id);
socket.on('chat-message', async (req) => {
const conversationId = req.conId;
console.log(conversationId);
socket.to(conversationId).emit('received', { message: req.message,time: Date.now() });
await Message.create({
time: Date.now(),
conversation: conversationId,
message: req.message,
receiver: req.secondUser,
});
});
socket.on('load', async (users) => {
let conversation = await Conversation.findOne({
participants: { $all: [users.firstUser, users.secondUser] },
});
if (!conversation) {
conversation = await Conversation.create({
participants: [users.firstUser, users.secondUser],
});
}
const conversationId = conversation._id;
socket.join(`${conversationId}`);
const oldMessages = await Message.find({
conversation: conversationId,
})
.sort('time')
.limit(100);
socket.emit('displayOldMessages', { oldMessages, conversationId });
});
});
};
//5fb8ba3bbe2f7734546a0fad
//5fbb8df9ca4cb540eced2d4a