-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (58 loc) · 1.95 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
const express = require("express");
const { connection } = require("./config/db");
const { userRoutes } = require("./routes/User.Routes");
const { groupRoutes } = require("./routes/Group.Routes");
const { UserModel } = require("./model/User.Model");
const { createServer } = require("http");
const { Server } = require("socket.io");
const jwt = require("jsonwebtoken");
const cors = require("cors");
require("dotenv").config();
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer);
app.use(express.json());
app.use(cors());
app.get("/", (req, res) => {
res.send("Server online. Make sure your ports are correct in '/scripts' - Made By Payson - github.com/paysonism");
});
app.use("/user", userRoutes);
app.use("/group", groupRoutes);
const obj = {};
io.on("connection", (socket) => {
console.log("Connection Established");
socket.on("createConnection", (userId) => {
obj[userId] = socket.id;
});
//server listening
socket.on("chatMsg", async (msg, receiverId, senderId) => {
let newMsg = {
message: msg,
senderId: senderId,
receiverId: receiverId,
};
await UserModel.updateOne(
{ _id: senderId },
{ $push: { chatMessageModel: newMsg } }
);
await UserModel.updateOne(
{ _id: receiverId },
{ $push: { chatMessageModel: newMsg } }
);
// to individual socketid (private message)
io.to(obj[receiverId]).emit("receivedMsg", msg, senderId);
});
socket.on("disconnect", () => {
console.log("Connection Terminated");
});
});
httpServer.listen(process.env.PORT, async () => {
try {
await connection;
console.log("Successfully Connected to the 1337 Server");
console.log("Server Port: " + process.env.PORT);
console.log("Made By Payson - github.com/paysonism");
} catch (error) {
console.log(error);
}
});