@jusbid/redis 1.0.4 → 1.0.6
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 +5 -4
- package/package.json +1 -1
- package/socket-manager.js +28 -3
package/index.js
CHANGED
|
@@ -8,9 +8,9 @@ const {
|
|
|
8
8
|
subscribe
|
|
9
9
|
} = require('./socketRedisClient');
|
|
10
10
|
|
|
11
|
-
const { initSocketManager, getIO, emitEvent } = require("./socket-manager");
|
|
11
|
+
const { initSocketManager, getIO, emitEvent, emitWithAck } = require("./socket-manager");
|
|
12
12
|
const { SOCKET_EVENTS } = require('./redis-client');
|
|
13
|
-
|
|
13
|
+
const { Server } = require("socket.io")
|
|
14
14
|
|
|
15
15
|
const redisOptions = {};
|
|
16
16
|
|
|
@@ -897,7 +897,7 @@ const createSocketServer = ({
|
|
|
897
897
|
origin: process.env.CORS_ORIGIN,
|
|
898
898
|
methods: ["GET", "POST"],
|
|
899
899
|
},
|
|
900
|
-
path = "/socket.io",
|
|
900
|
+
path = "/notification/socket.io",
|
|
901
901
|
verifyToken, // (token, socket) => Promise<payload> | payload
|
|
902
902
|
} = {}) => {
|
|
903
903
|
if (!httpServer) {
|
|
@@ -970,5 +970,6 @@ module.exports = {
|
|
|
970
970
|
|
|
971
971
|
initSocketManager, getIO, emitEvent,
|
|
972
972
|
createSocketServer,
|
|
973
|
-
SOCKET_EVENTS
|
|
973
|
+
SOCKET_EVENTS,
|
|
974
|
+
emitWithAck
|
|
974
975
|
};
|
package/package.json
CHANGED
package/socket-manager.js
CHANGED
|
@@ -23,17 +23,42 @@ const getIO = () => {
|
|
|
23
23
|
/**
|
|
24
24
|
* Emit from anywhere, without needing direct io access
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
|
|
27
|
+
const emitEvent = async (event, data, room = null) => {
|
|
27
28
|
const io = getIO();
|
|
28
29
|
if (room) {
|
|
30
|
+
const sockets = await io.in(room).fetchSockets(); // works across Redis Adapter instances too
|
|
31
|
+
if (sockets.length === 0) {
|
|
32
|
+
return { delivered: false, reason: "user_offline" };
|
|
33
|
+
}
|
|
29
34
|
io.to(room).emit(event, data);
|
|
30
|
-
|
|
31
|
-
io.emit(event, data);
|
|
35
|
+
return { delivered: true, socketCount: sockets.length };
|
|
32
36
|
}
|
|
37
|
+
io.emit(event, data);
|
|
38
|
+
return { delivered: true };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const emitWithAck = (room, event, data, timeoutMs = 5000) => {
|
|
42
|
+
const io = getIO();
|
|
43
|
+
|
|
44
|
+
return new Promise((resolve) => {
|
|
45
|
+
io.timeout(timeoutMs).to(room).emit(event, data, (err, responses) => {
|
|
46
|
+
console.log(responses)
|
|
47
|
+
if (err) {
|
|
48
|
+
// no client acked within timeout
|
|
49
|
+
resolve({ delivered: false, reason: "ack_timeout" });
|
|
50
|
+
} else {
|
|
51
|
+
// responses = array of whatever each acking client sent back
|
|
52
|
+
resolve({ delivered: true, acks: responses });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
33
56
|
};
|
|
34
57
|
|
|
58
|
+
|
|
35
59
|
module.exports = {
|
|
36
60
|
initSocketManager,
|
|
37
61
|
getIO,
|
|
38
62
|
emitEvent,
|
|
63
|
+
emitWithAck
|
|
39
64
|
};
|