shuttlepro-shared 1.1.71 → 1.1.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/socket.js +50 -69
- package/package.json +1 -1
package/config/socket.js
CHANGED
|
@@ -1,82 +1,66 @@
|
|
|
1
1
|
const { Server } = require("socket.io");
|
|
2
|
-
const { client: redisClient
|
|
3
|
-
const { createClient } = require("redis");
|
|
2
|
+
const { client: redisClient } = require("./redis"); // Import shared Redis client
|
|
4
3
|
require("dotenv").config();
|
|
5
4
|
|
|
6
5
|
let io;
|
|
6
|
+
const namespaceSockets = {}; // Store different namespace sockets
|
|
7
7
|
|
|
8
8
|
const initializeSocket = async (
|
|
9
9
|
server,
|
|
10
|
-
|
|
10
|
+
namespaceParam = "conversation",
|
|
11
11
|
redisChannel = "socket_events"
|
|
12
12
|
) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
io.listen(server); // Ensure server listens for WebSocket connections
|
|
13
|
+
if (!io) {
|
|
14
|
+
io = new Server(server, {
|
|
15
|
+
cors: { origin: "*" },
|
|
16
|
+
});
|
|
17
|
+
}
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
);
|
|
19
|
+
// Return existing namespace if already initialized
|
|
20
|
+
if (namespaceSockets[namespaceParam]) {
|
|
21
|
+
return namespaceSockets[namespaceParam];
|
|
22
|
+
}
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
// Create and store namespace
|
|
25
|
+
const namespace = io.of(`/${namespaceParam}`);
|
|
26
|
+
namespaceSockets[namespaceParam] = namespace;
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
// Ensure Redis client is connected
|
|
29
|
+
if (!redisClient.isOpen) {
|
|
30
|
+
await redisClient.connect();
|
|
31
|
+
}
|
|
34
32
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
// Subscribe to Redis events for this namespace
|
|
34
|
+
await redisClient.subscribe(redisChannel, (message) => {
|
|
35
|
+
try {
|
|
36
|
+
const { workspaceId, event, data } = JSON.parse(message);
|
|
37
|
+
namespace.to(workspaceId).emit(event, data);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
console.error("Error parsing Redis message:", err);
|
|
39
40
|
}
|
|
40
41
|
});
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const handleConnection = async (socket, namespace = null) => {
|
|
49
|
-
const { workspaceId } = socket.handshake.query;
|
|
50
|
-
|
|
51
|
-
if (!workspaceId) {
|
|
52
|
-
console.warn(`❌ Client ${socket.id} rejected due to missing workspaceId`);
|
|
53
|
-
socket.disconnect(true);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
console.log(
|
|
58
|
-
`✅ Client connected: ${socket.id} (Workspace: ${workspaceId}) Namespace: ${
|
|
59
|
-
namespace || "Global"
|
|
60
|
-
}`
|
|
61
|
-
);
|
|
43
|
+
// Handle new socket connections
|
|
44
|
+
namespace.on("connection", (socket) => {
|
|
45
|
+
const { workspaceId } = socket.handshake.query;
|
|
62
46
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
id: socket.id,
|
|
68
|
-
workspaceId,
|
|
69
|
-
namespace,
|
|
70
|
-
connectedAt: new Date().toISOString(),
|
|
71
|
-
})
|
|
72
|
-
);
|
|
47
|
+
if (!workspaceId) {
|
|
48
|
+
socket.disconnect(true);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
73
51
|
|
|
74
|
-
socket.on("disconnect", async () => {
|
|
75
52
|
console.log(
|
|
76
|
-
|
|
53
|
+
`✅ Client connected: ${socket.id} (Workspace: ${workspaceId}) in /${namespaceParam}`
|
|
77
54
|
);
|
|
78
|
-
|
|
55
|
+
|
|
56
|
+
socket.join(workspaceId); // Join workspace room
|
|
57
|
+
|
|
58
|
+
socket.on("disconnect", () => {
|
|
59
|
+
socket.leave(workspaceId);
|
|
60
|
+
});
|
|
79
61
|
});
|
|
62
|
+
|
|
63
|
+
return namespace;
|
|
80
64
|
};
|
|
81
65
|
|
|
82
66
|
const sendEventToServer = async ({
|
|
@@ -85,17 +69,14 @@ const sendEventToServer = async ({
|
|
|
85
69
|
data,
|
|
86
70
|
redisChannel = "socket_events",
|
|
87
71
|
}) => {
|
|
88
|
-
|
|
89
|
-
await redisClient.
|
|
72
|
+
try {
|
|
73
|
+
await redisClient.publish(
|
|
74
|
+
redisChannel,
|
|
75
|
+
JSON.stringify({ workspaceId, event, data })
|
|
76
|
+
);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.error("Error publishing to Redis:", err);
|
|
90
79
|
}
|
|
91
|
-
|
|
92
|
-
await redisClient.publish(
|
|
93
|
-
redisChannel,
|
|
94
|
-
JSON.stringify({ workspaceId, event, data })
|
|
95
|
-
);
|
|
96
80
|
};
|
|
97
81
|
|
|
98
|
-
module.exports = {
|
|
99
|
-
initializeSocket,
|
|
100
|
-
sendEventToServer,
|
|
101
|
-
};
|
|
82
|
+
module.exports = { initializeSocket, sendEventToServer };
|