-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (64 loc) · 1.9 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const dotenv = require("dotenv");
const { default: mongoose } = require("mongoose");
const app = express();
const httpServer = createServer(app);
const cors = require("cors");
const { notFound, errorHandler } = require("./middleware/errorMiddleware");
app.use(
cors({
origin: "*",
})
);
dotenv.config();
app.use(express.json());
const userRoutes = require("./Routes/userRoutes");
const chatRoutes = require("./Routes/chatRoutes");
const messageRoutes = require("./Routes/messageRoutes");
const connectDb = async () => {
try {
const connect = await mongoose.connect(process.env.MONGO_URI);
console.log("Server is Connected to Database");
} catch (err) {
console.log("Server is NOT connected to Database", err.message);
}
};
connectDb();
app.get("/", (req, res) => {
res.send("API is running123");
});
app.use("/user", userRoutes);
app.use("/chat", chatRoutes);
app.use("/message", messageRoutes);
// Error Handling middlewares
app.use(notFound);
app.use(errorHandler);
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, console.log("Server is Running..."));
const io = require("socket.io")(server, {
cors: {
origin: "*",
},
pingTimeout: 6000,
});
io.on("connection", (socket) => {
socket.on("setup", (user) => {
socket.join(user?.data._id);
socket.emit("Connected");
});
socket.on("join chat", (room) =>{
socket.join(room);
})
socket.on("new message", (newMessageStatus) => {
var chat = newMessageStatus.chat;
if(!chat.users) {
return console.log("chat.users not defined");
}
chat.users.foreach((user) => {
if(user._id == newMessageStatus.sender._id) return ;
socket.in(user._id).emit("message recieved", newMessageRecieved);
});
});
});