-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
69 lines (55 loc) · 2.24 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require('express');
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
const cors = require('cors');
const bodyParser = require('body-parser');
const { connectToDataBase } = require('./utils/connectToDB');
require('dotenv').config();
const { add_user, removeUser, getConnectUsers } = require('./utils/room');
const { getMessages, sendNewMessage, setMsgToRead } = require('./utils/messageActions');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
connectToDataBase(process.env.MONGO_URL);
io.on('connection', socket => {
socket.on('be_online', async ({ user_id }) => {
const users = await add_user(user_id, socket.id)
setInterval(() => {
socket.emit("connect_users", {
users: users.filter(user => user.user_id !== user_id)
})
}, 5000)
})
socket.on('$loading_messages$', async ({ user_id, messageWith }) => {
const { chat, error } = await getMessages(user_id, messageWith)
if (!error) {
socket.emit('$messages_loaded$', { chat })
}
})
socket.on('$sending_new_messages$', async ({ user_id, receiver_id, text }) => {
const { new_msg, error } = await sendNewMessage(user_id, receiver_id, text);
const reseiverSocket = getConnectUsers(receiver_id);
if (reseiverSocket) {
io.to(reseiverSocket.socket_id).emit('$new_messages_received$', new_msg);
} else {
await setMsgToRead(receiver_id);
}
if (!error) {
socket.emit('%_new_msg_sent%', new_msg)
}
})
socket.on('disconnect', () => removeUser(socket.id));
})
app.get('/', (req, res) => {
res.send('woooooooooooork')
})
app.use('/api/v1/auth', require('./routes/authRoutes'));
app.use('/api/v1/post', require('./routes/postRouter'));
app.use('/api/v1/profile', require('./routes/profileRoute'));
app.use('/api/v1/search', require('./routes/searchRoutes'));
app.use('/api/v1/notifications', require('./routes/notificationRoute'));
app.use('/api/v1/chats', require('./routes/chatRoutes'));
const PORT = process.env.PORT || 3500;
server.listen(PORT);
console.log(`Your app running on ${PORT}`)