shuttlepro-shared 1.1.72 → 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 +25 -37
- package/package.json +1 -1
package/config/socket.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { Server } = require("socket.io");
|
|
2
|
-
const {
|
|
2
|
+
const { client: redisClient } = require("./redis"); // Import shared Redis client
|
|
3
3
|
require("dotenv").config();
|
|
4
4
|
|
|
5
5
|
let io;
|
|
@@ -16,40 +16,35 @@ const initializeSocket = async (
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// Return existing namespace if already initialized
|
|
20
20
|
if (namespaceSockets[namespaceParam]) {
|
|
21
|
-
console.log(`⚡ Namespace ${namespaceParam} already initialized`);
|
|
22
21
|
return namespaceSockets[namespaceParam];
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
// Create
|
|
24
|
+
// Create and store namespace
|
|
26
25
|
const namespace = io.of(`/${namespaceParam}`);
|
|
27
26
|
namespaceSockets[namespaceParam] = namespace;
|
|
28
|
-
console.log(`✅ WebSocket Namespace Initialized: /${namespaceParam}`);
|
|
29
27
|
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
subClient.on("message", (channel, message) => {
|
|
36
|
-
console.log(
|
|
37
|
-
`🔔 Redis Event on ${channel} for /${namespaceParam}:`,
|
|
38
|
-
message
|
|
39
|
-
);
|
|
40
|
-
const { workspaceId, event, data } = JSON.parse(message);
|
|
28
|
+
// Ensure Redis client is connected
|
|
29
|
+
if (!redisClient.isOpen) {
|
|
30
|
+
await redisClient.connect();
|
|
31
|
+
}
|
|
41
32
|
|
|
42
|
-
|
|
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);
|
|
40
|
+
}
|
|
43
41
|
});
|
|
44
42
|
|
|
45
|
-
// Handle socket connections
|
|
43
|
+
// Handle new socket connections
|
|
46
44
|
namespace.on("connection", (socket) => {
|
|
47
45
|
const { workspaceId } = socket.handshake.query;
|
|
48
46
|
|
|
49
47
|
if (!workspaceId) {
|
|
50
|
-
console.warn(
|
|
51
|
-
`❌ Client ${socket.id} rejected due to missing workspaceId`
|
|
52
|
-
);
|
|
53
48
|
socket.disconnect(true);
|
|
54
49
|
return;
|
|
55
50
|
}
|
|
@@ -61,9 +56,7 @@ const initializeSocket = async (
|
|
|
61
56
|
socket.join(workspaceId); // Join workspace room
|
|
62
57
|
|
|
63
58
|
socket.on("disconnect", () => {
|
|
64
|
-
|
|
65
|
-
`❌ Client disconnected: ${socket.id} (Workspace: ${workspaceId}) in /${namespaceParam}`
|
|
66
|
-
);
|
|
59
|
+
socket.leave(workspaceId);
|
|
67
60
|
});
|
|
68
61
|
});
|
|
69
62
|
|
|
@@ -71,24 +64,19 @@ const initializeSocket = async (
|
|
|
71
64
|
};
|
|
72
65
|
|
|
73
66
|
const sendEventToServer = async ({
|
|
74
|
-
namespace = "conversation",
|
|
75
67
|
workspaceId,
|
|
76
68
|
event,
|
|
77
69
|
data,
|
|
78
70
|
redisChannel = "socket_events",
|
|
79
71
|
}) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
JSON.stringify({ workspaceId, event, data })
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
await redisClient.disconnect();
|
|
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);
|
|
79
|
+
}
|
|
92
80
|
};
|
|
93
81
|
|
|
94
82
|
module.exports = { initializeSocket, sendEventToServer };
|