-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (35 loc) Β· 1.12 KB
/
server.js
File metadata and controls
45 lines (35 loc) Β· 1.12 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
const app = require("./src/app");
const { createServer } = require("http");
const { Server } = require("socket.io");
const PORT = process.env.PORT || 5000;
const httpServer = createServer(app);
// Socket.io setup
const io = new Server(httpServer, {
cors: {
origin: "*", // Later restrict to frontend URL
methods: ["GET", "POST", "PUT", "DELETE"]
}
});
io.on("connection", (socket) => {
console.log("π User connected:", socket.id);
// Join a board room
socket.on("joinBoard", (boardId) => {
socket.join(boardId);
console.log(`π User ${socket.id} joined board ${boardId}`);
});
// Leave board room
socket.on("leaveBoard", (boardId) => {
socket.leave(boardId);
console.log(`πͺ User ${socket.id} left board ${boardId}`);
});
socket.on("joinUser", (userId) => {
socket.join(userId);
console.log(`π« User ${userId} joined personal room`);
});
socket.on("disconnect", () => {
console.log("β User disconnected:", socket.id);
});
});
// Make io accessible in controllers
app.set("io", io);
httpServer.listen(PORT, () => console.log(`π Server running on port ${PORT}`));