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.
Files changed (2) hide show
  1. package/config/socket.js +25 -37
  2. package/package.json +1 -1
package/config/socket.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { Server } = require("socket.io");
2
- const { createClient } = require("redis");
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
- // If namespace is already initialized, return it
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 new namespace
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
- // Create Redis subscriber for this namespace
31
- const subClient = createClient({ url: process.env.REDIS_URL });
32
- await subClient.connect();
33
- await subClient.subscribe(redisChannel);
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
- namespace.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);
40
+ }
43
41
  });
44
42
 
45
- // Handle socket connections per namespace
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
- console.log(
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
- const redisClient = createClient({ url: process.env.REDIS_URL });
81
- await redisClient.connect();
82
-
83
- console.log(
84
- `📤 Sending event '${event}' to namespace '/${namespace}' for workspace '${workspaceId}'`
85
- );
86
- await redisClient.publish(
87
- redisChannel,
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.1.72",
3
+ "version": "1.1.73",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {