@jusbid/redis 1.0.1 → 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 +19 -1
- package/package.json +1 -1
- package/redis-client.js +16 -0
- package/socket-manager.js +39 -0
- package/socketRedisClient.js +78 -0
package/index.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
const { createClient } = require('redis');
|
|
2
2
|
const { logger } = require('@jusbid/logger');
|
|
3
|
+
const {
|
|
4
|
+
closeRedisClients,
|
|
5
|
+
initRedisClients,
|
|
6
|
+
getRedisClients,
|
|
7
|
+
publish,
|
|
8
|
+
subscribe
|
|
9
|
+
} = require('./socketRedisClient');
|
|
10
|
+
|
|
11
|
+
const { initSocketManager, getIO, emitEvent } = require("./socket-manager");
|
|
12
|
+
|
|
3
13
|
|
|
4
14
|
const redisOptions = {};
|
|
5
15
|
|
|
@@ -900,5 +910,13 @@ module.exports = {
|
|
|
900
910
|
xDel,
|
|
901
911
|
xTrim,
|
|
902
912
|
xReadGroupNew,
|
|
903
|
-
deleteByPattern
|
|
913
|
+
deleteByPattern,
|
|
914
|
+
closeRedisClients,
|
|
915
|
+
initRedisClients,
|
|
916
|
+
getRedisClients,
|
|
917
|
+
publish,
|
|
918
|
+
subscribe,
|
|
919
|
+
|
|
920
|
+
initSocketManager, getIO, emitEvent
|
|
921
|
+
|
|
904
922
|
};
|
package/package.json
CHANGED
package/redis-client.js
ADDED
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
const { createClient } = require("redis");
|
|
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
|
+
}
|