-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
44 lines (36 loc) · 990 Bytes
/
Copy pathsocket.js
File metadata and controls
44 lines (36 loc) · 990 Bytes
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
// socket.js
let ioInstance;
function initSocketIO(server) {
const { Server } = require("socket.io");
ioInstance = new Server(server, {
cors: {
origin: "*", // adjust as needed
},
});
// ✅ Setup listeners once, inside this file
ioInstance.on("connection", (socket) => {
console.log("⚡ New client connected:", socket.id);
socket.on("join-user", (userId) => {
socket.join(userId);
console.log(`👤 User ${userId} joined their personal room`);
});
socket.on("join-store", (storeId) => {
socket.join(storeId);
console.log(`🏪 Admin joined store room ${storeId}`);
});
socket.on("disconnect", () => {
console.log("🔌 Client disconnected:", socket.id);
});
});
return ioInstance;
}
function getIO() {
if (!ioInstance) {
throw new Error("❌ Socket.IO not initialized");
}
return ioInstance;
}
module.exports = {
initSocketIO,
getIO
};