@jusbid/redis 1.0.5 → 1.0.7
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 +8 -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
|
-
const { Server } = require("socket.io")
|
|
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) {
|
|
@@ -907,7 +907,10 @@ const createSocketServer = ({
|
|
|
907
907
|
const socketServer = new Server(httpServer, {
|
|
908
908
|
cors,
|
|
909
909
|
path,
|
|
910
|
+
pingTimeout: 25000,
|
|
911
|
+
pingInterval: 20000, // good to set explicitly too
|
|
910
912
|
});
|
|
913
|
+
socketServer.adapter(createAdapter(getRedisClients()));
|
|
911
914
|
|
|
912
915
|
// Token handshake middleware
|
|
913
916
|
socketServer.use(async (socket, next) => {
|
|
@@ -970,5 +973,6 @@ module.exports = {
|
|
|
970
973
|
|
|
971
974
|
initSocketManager, getIO, emitEvent,
|
|
972
975
|
createSocketServer,
|
|
973
|
-
SOCKET_EVENTS
|
|
976
|
+
SOCKET_EVENTS,
|
|
977
|
+
emitWithAck
|
|
974
978
|
};
|
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
|
};
|