-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
116 lines (92 loc) · 2.97 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const path = require('path');
const cors = require('cors');
const bodyParser = require('body-parser');
const fs = require('fs');
require('dotenv').config();
const {
addUser,
removeUser,
findConnectedUser,
} = require('./controllers/chatRoom');
const {
loadMessages,
sendMsg,
deleteMsg,
} = require('./controllers/chatMessages');
// app
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
//socket.io
io.on('connection', (socket) => {
socket.on('join', async ({ userId }) => {
const users = await addUser(userId, socket.id);
//console.log(users);
setInterval(() => {
socket.emit('connectedUsers', {
users: users.filter((user) => user.userId !== userId),
});
}, 10000);
});
socket.on('loadMessages', async ({ userId, messagesWith }) => {
const { chat, error } = await loadMessages(userId, messagesWith);
!error
? socket.emit('messagesLoaded', { chat })
: socket.emit('noChatFound');
});
socket.on('sendNewMsg', async ({ userId, msgSendToUserId, msg }) => {
const { newMsg, error } = await sendMsg(userId, msgSendToUserId, msg);
const receiverSocket = findConnectedUser(msgSendToUserId);
if (receiverSocket) {
// SEND MESSAGE TO A PARTICULAR SOCKET
io.to(receiverSocket.socketId).emit('newMsgReceived', { newMsg });
} else {
socket.emit('setMsgToUnread', { msgSendToUserId });
}
!error && socket.emit('msgSent', { newMsg });
});
socket.on('deleteMsg', async ({ userId, messagesWith, messageId }) => {
const { success } = await deleteMsg(userId, messagesWith, messageId);
if (success) socket.emit('msgDeleted');
});
socket.on('disconnectUser', () => removeUser(socket.id));
});
//image directory
app.use('/images', express.static(path.join(__dirname, 'images')));
// db
mongoose
.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: true,
})
.then(() => console.log('Database Connected'))
.catch((err) => console.log(err));
//middlewares
app.use(cors());
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(express.json());
// Route middleware
fs.readdirSync('./routes').map((routes) =>
app.use('/api', require(`./routes/${routes}`))
);
// ... other server configuration
if (process.env.NODE_ENV === 'production') {
const clientBuildPath = path.join(__dirname, 'client', 'build');
// Serve static files from the React build folder
app.use(express.static(clientBuildPath));
// Serve the index.html file for all non-api routes
app.get('*', (req, res) => {
res.sendFile(path.join(clientBuildPath, 'index.html'));
});
}
const port = process.env.PORT || 8000;
server.listen(port, () => {
//console.log(`Server is running on port ${port}`);
});