@jusbid/redis 1.0.2 → 1.0.3

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/index.js CHANGED
@@ -1,12 +1,16 @@
1
1
  const { createClient } = require('redis');
2
2
  const { logger } = require('@jusbid/logger');
3
- const { closeRedisClients,
3
+ const {
4
+ closeRedisClients,
4
5
  initRedisClients,
5
6
  getRedisClients,
6
7
  publish,
7
8
  subscribe
8
9
  } = require('./socketRedisClient');
9
10
 
11
+ const { initSocketManager, getIO, emitEvent } = require("./socket-manager");
12
+
13
+
10
14
  const redisOptions = {};
11
15
 
12
16
  // Build Redis connection options dynamically
@@ -911,6 +915,8 @@ module.exports = {
911
915
  initRedisClients,
912
916
  getRedisClients,
913
917
  publish,
914
- subscribe
918
+ subscribe,
919
+
920
+ initSocketManager, getIO, emitEvent
915
921
 
916
922
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jusbid/redis",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "main": "index.js",
5
5
  "private": false,
6
6
  "dependencies": {
@@ -0,0 +1,16 @@
1
+ // redis-client.js
2
+ const { createClient } = require("@jusbid/redis");
3
+
4
+ const createRedisClients = async ({ url = process.env.REDIS_URL } = {}) => {
5
+ const pubClient = createClient({ url });
6
+ const subClient = pubClient.duplicate();
7
+
8
+ pubClient.on("error", (err) => console.error("Redis Pub Client Error:", err));
9
+ subClient.on("error", (err) => console.error("Redis Sub Client Error:", err));
10
+
11
+ await Promise.all([pubClient.connect(), subClient.connect()]);
12
+
13
+ return { pubClient, subClient };
14
+ };
15
+
16
+ module.exports = { createRedisClients };
@@ -0,0 +1,39 @@
1
+ let ioInstance = null;
2
+
3
+ /**
4
+ * Initialize and store the io instance (call this ONCE, in index.js)
5
+ */
6
+ const initSocketManager = (socketServerInstance) => {
7
+ if (!socketServerInstance) {
8
+ throw new Error("Socket server instance is required to initialize manager");
9
+ }
10
+ ioInstance = socketServerInstance;
11
+ };
12
+
13
+ /**
14
+ * Get the stored io instance (call this from ANY other file)
15
+ */
16
+ const getIO = () => {
17
+ if (!ioInstance) {
18
+ throw new Error("Socket.IO not initialized. Call initSocketManager first.");
19
+ }
20
+ return ioInstance;
21
+ };
22
+
23
+ /**
24
+ * Emit from anywhere, without needing direct io access
25
+ */
26
+ const emitEvent = (event, data, room = null) => {
27
+ const io = getIO();
28
+ if (room) {
29
+ io.to(room).emit(event, data);
30
+ } else {
31
+ io.emit(event, data);
32
+ }
33
+ };
34
+
35
+ module.exports = {
36
+ initSocketManager,
37
+ getIO,
38
+ emitEvent,
39
+ };
@@ -1,4 +1,4 @@
1
-
1
+ const { createClient } = require("redis");
2
2
  let pubClient = null;
3
3
  let subClient = null;
4
4
  let isInitialized = false;