-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (83 loc) · 2.55 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Server } from "socket.io";
import { createServer } from "http";
import connectDB from "./db.js";
import next from "next";
await connectDB();
const dev = process.env.NODE_ENV !== "production";
const domain = process.env.DOMAIN;
const port = process.env.PORT;
const app = next({ dev, domain, port });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer(handler);
const io = new Server(server, {
cors: {
origin: "*",
credentials: true,
methods: ["GET", "POST"],
},
});
const userIdToSocketMap = {};
io.on("connection", async (socket) => {
const { userId } = socket.handshake.query;
if (userId) {
userIdToSocketMap[userId] = socket.id;
console.log(
`User with userId: ${userId} connected with socket id: ${socket.id}`
);
}
socket.on("disconnect", () => {
if (userId) {
delete userIdToSocketMap[userId];
console.log(`User with userId: ${userId} disconnected`);
}
});
socket.on("new-chat", ({ sidebarId, participants, lastMessage }) => {
console.log("New chat created:", sidebarId);
const unreadCount = 1; // Default unread count for the recipient
participants.forEach((participantId) => {
if (
userIdToSocketMap[participantId] &&
userIdToSocketMap[participantId] !== socket.id
) {
socket.to(userIdToSocketMap[participantId]).emit("receive-new-chat", {
sidebarId,
participants,
lastMessage, // Include last message details
unreadCount, // Send unread count
});
}
});
});
socket.on("send-message", (data) => {
const { room, sender, content,readBy, participants } = data;
// Log message for debugging
console.log(
`Message from ${sender}: ${content} to participants:`,
participants
);
// Emit the message to all participants except the sender
participants.forEach((participantId) => {
if (participantId !== sender) {
const participantSocket = userIdToSocketMap[participantId];
if (participantSocket) {
socket.to(participantSocket).emit("receive-message", {
room,
sender,
content,
readBy,
});
}
}
});
});
});
server
.once("error", (err) => {
console.error(err);
process.exit(1);
})
.listen(port, () => {
console.log(`Ready on http://${domain}:${port}`);
});
});