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.
Files changed (2) hide show
  1. package/config/socket.js +50 -69
  2. 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, connectRedis } = require("./redis");
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
- namespace = null,
10
+ namespaceParam = "conversation",
11
11
  redisChannel = "socket_events"
12
12
  ) => {
13
- await connectRedis(); // Ensure Redis is connected
14
-
15
- io = new Server(server, {
16
- cors: { origin: "*" },
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
- console.log(
22
- `✅ WebSocket Server Initialized ${
23
- namespace ? `with namespace: ${namespace}` : "globally"
24
- }, using Redis channel: ${redisChannel}`
25
- );
19
+ // Return existing namespace if already initialized
20
+ if (namespaceSockets[namespaceParam]) {
21
+ return namespaceSockets[namespaceParam];
22
+ }
26
23
 
27
- const subClient = createClient({ url: process.env.REDIS_URL });
28
- await subClient.connect();
29
- await subClient.subscribe(redisChannel);
24
+ // Create and store namespace
25
+ const namespace = io.of(`/${namespaceParam}`);
26
+ namespaceSockets[namespaceParam] = namespace;
30
27
 
31
- subClient.on("message", (channel, message) => {
32
- console.log(`🔔 Received event from Redis channel ${channel}:`, message);
33
- const { workspaceId, event, data } = JSON.parse(message);
28
+ // Ensure Redis client is connected
29
+ if (!redisClient.isOpen) {
30
+ await redisClient.connect();
31
+ }
34
32
 
35
- if (namespace) {
36
- io.of(namespace).to(workspaceId).emit(event, data);
37
- } else {
38
- io.to(workspaceId).emit(event, data);
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
- 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
- }
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
- 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
- );
47
+ if (!workspaceId) {
48
+ socket.disconnect(true);
49
+ return;
50
+ }
73
51
 
74
- socket.on("disconnect", async () => {
75
52
  console.log(
76
- `❌ Client disconnected: ${socket.id} (Workspace: ${workspaceId})`
53
+ `✅ Client connected: ${socket.id} (Workspace: ${workspaceId}) in /${namespaceParam}`
77
54
  );
78
- await redisClient.hDel(`${workspaceId}:sockets`, socket.id);
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
- if (!redisClient.isReady) {
89
- await redisClient.connect();
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.1.71",
3
+ "version": "1.1.73",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {