shuttlepro-shared 1.1.70 → 1.1.71
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 +9 -24
- package/package.json +1 -1
package/config/socket.js
CHANGED
|
@@ -5,12 +5,6 @@ require("dotenv").config();
|
|
|
5
5
|
|
|
6
6
|
let io;
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
* ✅ Initialize WebSocket Server with Redis Pub/Sub
|
|
10
|
-
* @param {Object} server - HTTP Server instance
|
|
11
|
-
* @param {string|null} namespace - Optional namespace (passed from `index.js`)
|
|
12
|
-
* @param {string} [redisChannel="socket_events"] - Custom Redis Pub/Sub Channel
|
|
13
|
-
*/
|
|
14
8
|
const initializeSocket = async (
|
|
15
9
|
server,
|
|
16
10
|
namespace = null,
|
|
@@ -22,19 +16,20 @@ const initializeSocket = async (
|
|
|
22
16
|
cors: { origin: "*" },
|
|
23
17
|
});
|
|
24
18
|
|
|
19
|
+
io.listen(server); // Ensure server listens for WebSocket connections
|
|
20
|
+
|
|
25
21
|
console.log(
|
|
26
22
|
`✅ WebSocket Server Initialized ${
|
|
27
23
|
namespace ? `with namespace: ${namespace}` : "globally"
|
|
28
24
|
}, using Redis channel: ${redisChannel}`
|
|
29
25
|
);
|
|
30
26
|
|
|
31
|
-
// Create Redis Pub/Sub Clients
|
|
32
|
-
const pubClient = redisClient;
|
|
33
27
|
const subClient = createClient({ url: process.env.REDIS_URL });
|
|
34
28
|
await subClient.connect();
|
|
29
|
+
await subClient.subscribe(redisChannel);
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
subClient.on("message", (channel, message) => {
|
|
32
|
+
console.log(`🔔 Received event from Redis channel ${channel}:`, message);
|
|
38
33
|
const { workspaceId, event, data } = JSON.parse(message);
|
|
39
34
|
|
|
40
35
|
if (namespace) {
|
|
@@ -44,18 +39,12 @@ const initializeSocket = async (
|
|
|
44
39
|
}
|
|
45
40
|
});
|
|
46
41
|
|
|
47
|
-
// ✅ Handle WebSocket Connection (Global or Namespace)
|
|
48
42
|
const socketServer = namespace ? io.of(namespace) : io;
|
|
49
43
|
socketServer.on("connection", (socket) =>
|
|
50
44
|
handleConnection(socket, namespace)
|
|
51
45
|
);
|
|
52
46
|
};
|
|
53
47
|
|
|
54
|
-
/**
|
|
55
|
-
* ✅ Handle WebSocket Connection
|
|
56
|
-
* @param {Object} socket - WebSocket client socket
|
|
57
|
-
* @param {string} [namespace] - Optional namespace
|
|
58
|
-
*/
|
|
59
48
|
const handleConnection = async (socket, namespace = null) => {
|
|
60
49
|
const { workspaceId } = socket.handshake.query;
|
|
61
50
|
|
|
@@ -82,7 +71,6 @@ const handleConnection = async (socket, namespace = null) => {
|
|
|
82
71
|
})
|
|
83
72
|
);
|
|
84
73
|
|
|
85
|
-
// ✅ Handle Disconnection
|
|
86
74
|
socket.on("disconnect", async () => {
|
|
87
75
|
console.log(
|
|
88
76
|
`❌ Client disconnected: ${socket.id} (Workspace: ${workspaceId})`
|
|
@@ -91,19 +79,16 @@ const handleConnection = async (socket, namespace = null) => {
|
|
|
91
79
|
});
|
|
92
80
|
};
|
|
93
81
|
|
|
94
|
-
/**
|
|
95
|
-
* ✅ Publish WebSocket Events via Redis
|
|
96
|
-
* @param {string} workspaceId - Target workspace ID
|
|
97
|
-
* @param {string} event - Event name
|
|
98
|
-
* @param {Object} data - Event payload
|
|
99
|
-
* @param {string} [redisChannel="socket_events"] - Custom Redis Pub/Sub Channel
|
|
100
|
-
*/
|
|
101
82
|
const sendEventToServer = async ({
|
|
102
83
|
workspaceId,
|
|
103
84
|
event,
|
|
104
85
|
data,
|
|
105
86
|
redisChannel = "socket_events",
|
|
106
87
|
}) => {
|
|
88
|
+
if (!redisClient.isReady) {
|
|
89
|
+
await redisClient.connect();
|
|
90
|
+
}
|
|
91
|
+
|
|
107
92
|
await redisClient.publish(
|
|
108
93
|
redisChannel,
|
|
109
94
|
JSON.stringify({ workspaceId, event, data })
|