-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.77 KB
/
index.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
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const socketio = require("socket.io");
const http = require("http");
const user = require("./server/routes/user.router");
const chatRoom = require("./server/routes/chatRoom.router");
const message = require("./server/routes/message.router");
const search = require("./server/routes/search.router");
const {
routeNotFound,
} = require("./server/middlewares/route-not-found.middleware");
const {
errorHandler,
} = require("./server/middlewares/error-handler.middleware");
const { initializeDBConnection } = require("./server/db/db.connect");
const { generateSearchIndex } = require("./server/utils/generateSearchIndex");
const { addUser, removeUser, getReceiver } = require("./server/utils/socket");
const port = process.env.PORT || 4444;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
io.on("connection", (socket) => {
socket.on("saveUser", (userId) => addUser(userId, socket.id));
socket.on("disconnect", () => removeUser(socket.id));
socket.on("sendMessage", ({ message, receiverId, room }) => {
const receiverSocketId = getReceiver(receiverId);
if (message) {
io.to(receiverSocketId).emit("getMessage", { message });
}
if (room) {
io.to(receiverSocketId).emit("getMessage", { room });
}
});
});
app.use(express.json());
app.use(cors());
initializeDBConnection();
app.use("/user", user);
app.use("/room", chatRoom);
app.use("/message", message);
app.use("/search", search);
/* Error handlers do not move should be at the end*/
app.use(routeNotFound);
app.use(errorHandler);
server.listen(port, () => {
console.log("server started on port " + port);
});
generateSearchIndex();