-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (52 loc) · 1.92 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
const app = require('./index');
const cors = require('cors')
const args = require('./utils/minimist');
const clusterMode = require('./utils/clusterMode');
const MongoContainer = require("./models/containers/mongo.container");
const cluster = require('cluster')
const envConfig = require('./config')
const os = require('os');
const { Server: HttpServer } = require('http');
const { Server: SocketServer } = require('socket.io');
const logger = require('./middlewares/logs.middleware');
const MessageServices = require('./services/messages.service');
const Message = new MessageServices();
const PORT = args.PORT || 8080;
// Socket.io
const httpServer = new HttpServer(app);
const io = new SocketServer(httpServer)
app.use(cors())
if (clusterMode && cluster.isMaster) {
const cpus = os.cpus().length;
for (let i = 0; i < cpus; i++) {
cluster.fork();
}
cluster.on('exit', worker => {
console.log('Worker', worker.process.pid, 'died', new Date().toLocaleDateString());
cluster.fork()
})
} else {
// Listen
logger.info(`Worker process ${process.pid} is running.`);
httpServer.listen(PORT, () => {
if (!["memory", "firebase"].includes(envConfig.DATASOURCE || "")) {
MongoContainer.connect().then(() => {
console.log("Connected to " + envConfig.DATASOURCE);
})
}
});
httpServer.on('error', error => {
console.error(`Error: ${error}`)
})
io.on('connection', async socket => {
logger.info(`New client connection - ID: ${socket.id}`)
// Getting all messages
const messages = await Message.getMessages()
socket.emit('messages', { messages })
// New Message
socket.on('new-message', async () => {
const messages = await Message.getMessages()
io.emit('messages', { messages })
})
})
}