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.
Files changed (2) hide show
  1. package/config/socket.js +52 -59
  2. 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
- namespace = null,
10
+ namespaceParam = "conversation",
11
11
  redisChannel = "socket_events"
12
12
  ) => {
13
- await connectRedis(); // Ensure Redis is connected
13
+ if (!io) {
14
+ io = new Server(server, {
15
+ cors: { origin: "*" },
16
+ });
17
+ }
14
18
 
15
- io = new Server(server, {
16
- cors: { origin: "*" },
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
- io.listen(server); // Ensure server listens for WebSocket connections
20
-
21
- console.log(
22
- `✅ WebSocket Server Initialized ${
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(`🔔 Received event from Redis channel ${channel}:`, message);
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
- if (namespace) {
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
- const socketServer = namespace ? io.of(namespace) : io;
43
- socketServer.on("connection", (socket) =>
44
- handleConnection(socket, namespace)
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
- console.log(
58
- `✅ Client connected: ${socket.id} (Workspace: ${workspaceId}) Namespace: ${
59
- namespace || "Global"
60
- }`
61
- );
62
-
63
- await redisClient.hSet(
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
- `❌ Client disconnected: ${socket.id} (Workspace: ${workspaceId})`
58
+ `✅ Client connected: ${socket.id} (Workspace: ${workspaceId}) in /${namespaceParam}`
77
59
  );
78
- await redisClient.hDel(`${workspaceId}:sockets`, socket.id);
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
- if (!redisClient.isReady) {
89
- await redisClient.connect();
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
- module.exports = {
99
- initializeSocket,
100
- sendEventToServer,
91
+ await redisClient.disconnect();
101
92
  };
93
+
94
+ module.exports = { initializeSocket, sendEventToServer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.1.71",
3
+ "version": "1.1.72",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {