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