-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (46 loc) · 1.53 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
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict'
const mongoose = require('mongoose');
const Chat = require('./chat-schema.js');
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
const MONGODB_URI = process.env.MONGODB_URI;
const { Server } = require('socket.io');
const PORT = process.env.PORT || 3000;
let users = [];
mongoose.connect(MONGODB_URI, { useNewParser: true, useUnifiedTopology: true }).then(() => {
console.log('mongoDB is connected');
}).catch(err => console.log(err));
const io = new Server(PORT, {cors: {origin: ['http://localhost:3001']}})
io.on('connection', (socket) => {
console.log('connected')
socket.on('add user', async (event) => {
users.push(event);
const allMessages = await Chat.find({ });
users[event] = event.socketID;
socket.emit('message list', { currentUser: event, allMessages });
});
socket.on('message1',(event) => {
const message = new Chat( event );
message.save().then(() => {
io.emit('message saved to db');
})
socket.emit('message', message);
});
socket.on('private message', (event) => {
const message = new Chat( event );
message.save().then(() => {
io.emit('message saved to db');
console.log('message saved to DB');
})
io.to(users[message.privateReceiver]).emit('private message', message);
socket.emit('private message', message);
});
socket.on('disconnect', (event) => {
console.log('user has left the chat');
return event;
});
});