shuttlepro-shared 1.1.71 → 1.1.72
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 +52 -59
- package/package.json +1 -1
package/config/socket.js
CHANGED
|
@@ -1,101 +1,94 @@
|
|
|
1
1
|
const { Server } = require("socket.io");
|
|
2
|
-
const { client: redisClient, connectRedis } = require("./redis");
|
|
3
2
|
const { createClient } = require("redis");
|
|
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
|
-
|
|
13
|
+
if (!io) {
|
|
14
|
+
io = new Server(server, {
|
|
15
|
+
cors: { origin: "*" },
|
|
16
|
+
});
|
|
17
|
+
}
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
// If namespace is already initialized, return it
|
|
20
|
+
if (namespaceSockets[namespaceParam]) {
|
|
21
|
+
console.log(`⚡ Namespace ${namespaceParam} already initialized`);
|
|
22
|
+
return namespaceSockets[namespaceParam];
|
|
23
|
+
}
|
|
18
24
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
namespace ? `with namespace: ${namespace}` : "globally"
|
|
24
|
-
}, using Redis channel: ${redisChannel}`
|
|
25
|
-
);
|
|
25
|
+
// Create new namespace
|
|
26
|
+
const namespace = io.of(`/${namespaceParam}`);
|
|
27
|
+
namespaceSockets[namespaceParam] = namespace;
|
|
28
|
+
console.log(`✅ WebSocket Namespace Initialized: /${namespaceParam}`);
|
|
26
29
|
|
|
30
|
+
// Create Redis subscriber for this namespace
|
|
27
31
|
const subClient = createClient({ url: process.env.REDIS_URL });
|
|
28
32
|
await subClient.connect();
|
|
29
33
|
await subClient.subscribe(redisChannel);
|
|
30
34
|
|
|
31
35
|
subClient.on("message", (channel, message) => {
|
|
32
|
-
console.log(
|
|
36
|
+
console.log(
|
|
37
|
+
`🔔 Redis Event on ${channel} for /${namespaceParam}:`,
|
|
38
|
+
message
|
|
39
|
+
);
|
|
33
40
|
const { workspaceId, event, data } = JSON.parse(message);
|
|
34
41
|
|
|
35
|
-
|
|
36
|
-
io.of(namespace).to(workspaceId).emit(event, data);
|
|
37
|
-
} else {
|
|
38
|
-
io.to(workspaceId).emit(event, data);
|
|
39
|
-
}
|
|
42
|
+
namespace.to(workspaceId).emit(event, data);
|
|
40
43
|
});
|
|
41
44
|
|
|
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
|
-
}
|
|
45
|
+
// Handle socket connections per namespace
|
|
46
|
+
namespace.on("connection", (socket) => {
|
|
47
|
+
const { workspaceId } = socket.handshake.query;
|
|
56
48
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
`${workspaceId}:sockets`,
|
|
65
|
-
socket.id,
|
|
66
|
-
JSON.stringify({
|
|
67
|
-
id: socket.id,
|
|
68
|
-
workspaceId,
|
|
69
|
-
namespace,
|
|
70
|
-
connectedAt: new Date().toISOString(),
|
|
71
|
-
})
|
|
72
|
-
);
|
|
49
|
+
if (!workspaceId) {
|
|
50
|
+
console.warn(
|
|
51
|
+
`❌ Client ${socket.id} rejected due to missing workspaceId`
|
|
52
|
+
);
|
|
53
|
+
socket.disconnect(true);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
73
56
|
|
|
74
|
-
socket.on("disconnect", async () => {
|
|
75
57
|
console.log(
|
|
76
|
-
|
|
58
|
+
`✅ Client connected: ${socket.id} (Workspace: ${workspaceId}) in /${namespaceParam}`
|
|
77
59
|
);
|
|
78
|
-
|
|
60
|
+
|
|
61
|
+
socket.join(workspaceId); // Join workspace room
|
|
62
|
+
|
|
63
|
+
socket.on("disconnect", () => {
|
|
64
|
+
console.log(
|
|
65
|
+
`❌ Client disconnected: ${socket.id} (Workspace: ${workspaceId}) in /${namespaceParam}`
|
|
66
|
+
);
|
|
67
|
+
});
|
|
79
68
|
});
|
|
69
|
+
|
|
70
|
+
return namespace;
|
|
80
71
|
};
|
|
81
72
|
|
|
82
73
|
const sendEventToServer = async ({
|
|
74
|
+
namespace = "conversation",
|
|
83
75
|
workspaceId,
|
|
84
76
|
event,
|
|
85
77
|
data,
|
|
86
78
|
redisChannel = "socket_events",
|
|
87
79
|
}) => {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
80
|
+
const redisClient = createClient({ url: process.env.REDIS_URL });
|
|
81
|
+
await redisClient.connect();
|
|
91
82
|
|
|
83
|
+
console.log(
|
|
84
|
+
`📤 Sending event '${event}' to namespace '/${namespace}' for workspace '${workspaceId}'`
|
|
85
|
+
);
|
|
92
86
|
await redisClient.publish(
|
|
93
87
|
redisChannel,
|
|
94
88
|
JSON.stringify({ workspaceId, event, data })
|
|
95
89
|
);
|
|
96
|
-
};
|
|
97
90
|
|
|
98
|
-
|
|
99
|
-
initializeSocket,
|
|
100
|
-
sendEventToServer,
|
|
91
|
+
await redisClient.disconnect();
|
|
101
92
|
};
|
|
93
|
+
|
|
94
|
+
module.exports = { initializeSocket, sendEventToServer };
|