@jusbid/redis 1.0.0 → 1.0.2

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,5 +1,11 @@
1
1
  const { createClient } = require('redis');
2
2
  const { logger } = require('@jusbid/logger');
3
+ const { closeRedisClients,
4
+ initRedisClients,
5
+ getRedisClients,
6
+ publish,
7
+ subscribe
8
+ } = require('./socketRedisClient');
3
9
 
4
10
  const redisOptions = {};
5
11
 
@@ -866,6 +872,16 @@ const xReadGroupNew = async (...args) => {
866
872
  }
867
873
  };
868
874
 
875
+
876
+ const deleteByPattern = async (pattern) => {
877
+ const keys = await redisClient.keys(pattern)
878
+ if (keys.length > 0) {
879
+ await redisClient.del(keys);
880
+ }
881
+
882
+ return keys.length;
883
+ }
884
+
869
885
  module.exports = {
870
886
  redisClient,
871
887
  get,
@@ -889,5 +905,12 @@ module.exports = {
889
905
  xAutoClaim,
890
906
  xDel,
891
907
  xTrim,
892
- xReadGroupNew
908
+ xReadGroupNew,
909
+ deleteByPattern,
910
+ closeRedisClients,
911
+ initRedisClients,
912
+ getRedisClients,
913
+ publish,
914
+ subscribe
915
+
893
916
  };
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@jusbid/redis",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "private": false,
6
6
  "dependencies": {
7
7
  "redis": "^4.6.13",
8
8
  "@jusbid/logger": "1.0.0"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
9
12
  }
10
13
  }
@@ -0,0 +1,78 @@
1
+
2
+ let pubClient = null;
3
+ let subClient = null;
4
+ let isInitialized = false;
5
+
6
+ /**
7
+ * Initialize Redis clients ONCE, at app boot.
8
+ * pubClient: used for adapter broadcast + regular publish()
9
+ * subClient: used for adapter + regular subscribe()
10
+ */
11
+ const initRedisClients = async ({ url = process.env.REDIS_URL } = {}) => {
12
+ if (isInitialized) {
13
+ return { pubClient, subClient };
14
+ }
15
+
16
+ pubClient = createClient({ url });
17
+ subClient = pubClient.duplicate();
18
+
19
+ pubClient.on("error", (err) => console.error("Redis Pub Client Error:", err.message));
20
+ subClient.on("error", (err) => console.error("Redis Sub Client Error:", err.message));
21
+
22
+ await Promise.all([pubClient.connect(), subClient.connect()]);
23
+
24
+ isInitialized = true;
25
+ console.log("Redis clients connected");
26
+
27
+ return { pubClient, subClient };
28
+ };
29
+
30
+ /**
31
+ * Get raw clients — needed by the Socket.IO Redis Adapter
32
+ */
33
+ const getRedisClients = () => {
34
+ if (!isInitialized) {
35
+ throw new Error("Redis not initialized. Call initRedisClients first.");
36
+ }
37
+ return { pubClient, subClient };
38
+ };
39
+
40
+ /**
41
+ * Publish a message on any channel — usable from any file
42
+ */
43
+ const publish = async (channel, payload) => {
44
+ if (!isInitialized) {
45
+ throw new Error("Redis not initialized. Call initRedisClients first.");
46
+ }
47
+ await pubClient.publish(channel, JSON.stringify(payload));
48
+ };
49
+
50
+ /**
51
+ * Subscribe to a channel with a handler — usable from any file
52
+ */
53
+ const subscribe = async (channel, handler) => {
54
+ if (!isInitialized) {
55
+ throw new Error("Redis not initialized. Call initRedisClients first.");
56
+ }
57
+ await subClient.subscribe(channel, (message) => {
58
+ try {
59
+ handler(JSON.parse(message));
60
+ } catch (err) {
61
+ console.error(`Failed to handle message on "${channel}":`, err.message);
62
+ }
63
+ });
64
+ };
65
+
66
+ const closeRedisClients = async () => {
67
+ if (pubClient?.isOpen) await pubClient.quit();
68
+ if (subClient?.isOpen) await subClient.quit();
69
+ isInitialized = false;
70
+ };
71
+
72
+ module.exports = {
73
+ closeRedisClients,
74
+ initRedisClients,
75
+ getRedisClients,
76
+ publish,
77
+ subscribe
78
+ }