-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (48 loc) · 1.41 KB
/
server.js
File metadata and controls
53 lines (48 loc) · 1.41 KB
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
import app from "./app.js";
import logger from "./configs/logger.config.js";
import dbConnection from "./configs/database.config.js";
import { Server } from "socket.io";
import Socketserver from "./socketServer.js";
const PORT = process.env.PORT || 8000;
// Database connection
dbConnection();
// Start HTTP Server
const server = app.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`);
});
// Initialize Socket.IO
const io = new Server(server, {
pingTimeout: 60000,
cors: {
origin: process.env.CLIENT_ENDPOINT || "http://localhost:3000",
},
});
// event when client connect to server
io.on("connection", (socket) => {
logger.info(`User connected: ${socket.id}`);
// socket is client-specific, meaning it represents a single client's connection.
//? io represents the entire server and can send messages to all clients or specific groups of clients (rooms).
Socketserver(socket, io);
});
//! Handle Server Errors
const exitHandler = () => {
if (server) {
logger.info("Server closed");
process.exit(1);
} else {
process.exit(1);
}
};
const unexpectedErrorHandler = (error) => {
logger.error(error);
exitHandler();
};
// console.table(process.memoryUsage());
process.on("uncaughtException", unexpectedErrorHandler);
process.on("unhandledRejection", unexpectedErrorHandler);
process.on("SIGTERM", () => {
if (server) {
logger.info("Server closed");
process.exit(1);
}
});