@socket.io/cluster-adapter
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -21,2 +21,3 @@ import { Adapter, BroadcastOptions, Room } from "socket.io-adapter"; | ||
private requests; | ||
private ackRequests; | ||
/** | ||
@@ -42,2 +43,4 @@ * Adapter constructor. | ||
broadcast(packet: any, opts: BroadcastOptions): void; | ||
broadcastWithAck(packet: any, opts: BroadcastOptions, clientCountCallback: (clientCount: number) => void, ack: (...args: any[]) => void): void; | ||
serverCount(): Promise<number>; | ||
addSockets(opts: BroadcastOptions, rooms: Room[]): void; | ||
@@ -44,0 +47,0 @@ delSockets(opts: BroadcastOptions, rooms: Room[]): void; |
@@ -27,2 +27,4 @@ "use strict"; | ||
EventType[EventType["SERVER_SIDE_EMIT_RESPONSE"] = 11] = "SERVER_SIDE_EMIT_RESPONSE"; | ||
EventType[EventType["BROADCAST_CLIENT_COUNT"] = 12] = "BROADCAST_CLIENT_COUNT"; | ||
EventType[EventType["BROADCAST_ACK"] = 13] = "BROADCAST_ACK"; | ||
})(EventType || (EventType = {})); | ||
@@ -55,2 +57,3 @@ /** | ||
this.requests = new Map(); | ||
this.ackRequests = new Map(); | ||
this.requestsTimeout = opts.requestsTimeout || 5000; | ||
@@ -92,5 +95,39 @@ this.publish({ | ||
debug("broadcast with opts %j", message.data.opts); | ||
super.broadcast(message.data.packet, ClusterAdapter.deserializeOptions(message.data.opts)); | ||
const withAck = message.data.requestId !== undefined; | ||
if (withAck) { | ||
super.broadcastWithAck(message.data.packet, ClusterAdapter.deserializeOptions(message.data.opts), (clientCount) => { | ||
debug("waiting for %d client acknowledgements", clientCount); | ||
this.publish({ | ||
type: EventType.BROADCAST_CLIENT_COUNT, | ||
data: { | ||
requestId: message.data.requestId, | ||
clientCount, | ||
}, | ||
}); | ||
}, (arg) => { | ||
debug("received acknowledgement with value %j", arg); | ||
this.publish({ | ||
type: EventType.BROADCAST_ACK, | ||
data: { | ||
requestId: message.data.requestId, | ||
packet: arg, | ||
}, | ||
}); | ||
}); | ||
} | ||
else { | ||
super.broadcast(message.data.packet, ClusterAdapter.deserializeOptions(message.data.opts)); | ||
} | ||
break; | ||
} | ||
case EventType.BROADCAST_CLIENT_COUNT: { | ||
const request = this.ackRequests.get(message.data.requestId); | ||
request === null || request === void 0 ? void 0 : request.clientCountCallback(message.data.clientCount); | ||
break; | ||
} | ||
case EventType.BROADCAST_ACK: { | ||
const request = this.ackRequests.get(message.data.requestId); | ||
request === null || request === void 0 ? void 0 : request.ack(message.data.packet); | ||
break; | ||
} | ||
case EventType.SOCKETS_JOIN: { | ||
@@ -231,2 +268,34 @@ debug("calling addSockets with opts %j", message.data.opts); | ||
} | ||
broadcastWithAck(packet, opts, clientCountCallback, ack) { | ||
var _a; | ||
const onlyLocal = (_a = opts === null || opts === void 0 ? void 0 : opts.flags) === null || _a === void 0 ? void 0 : _a.local; | ||
if (!onlyLocal) { | ||
const requestId = randomId(); | ||
this.publish({ | ||
type: EventType.BROADCAST, | ||
data: { | ||
packet, | ||
requestId, | ||
opts: ClusterAdapter.serializeOptions(opts), | ||
}, | ||
}); | ||
this.ackRequests.set(requestId, { | ||
type: EventType.BROADCAST, | ||
clientCountCallback, | ||
ack, | ||
}); | ||
// we have no way to know at this level whether the server has received an acknowledgement from each client, so we | ||
// will simply clean up the ackRequests map after the given delay | ||
setTimeout(() => { | ||
this.ackRequests.delete(requestId); | ||
}, opts.flags.timeout); | ||
} | ||
// packets with binary contents are modified by the broadcast method, hence the nextTick() | ||
process.nextTick(() => { | ||
super.broadcastWithAck(packet, opts, clientCountCallback, ack); | ||
}); | ||
} | ||
serverCount() { | ||
return Promise.resolve(1 + this.workerIds.size); | ||
} | ||
addSockets(opts, rooms) { | ||
@@ -233,0 +302,0 @@ var _a; |
{ | ||
"name": "@socket.io/cluster-adapter", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "The Socket.IO cluster adapter, allowing to broadcast events between several Socket.IO servers", | ||
@@ -23,3 +23,3 @@ "license": "MIT", | ||
"debug": "~4.3.1", | ||
"socket.io-adapter": "~2.3.0" | ||
"socket.io-adapter": "~2.4.0" | ||
}, | ||
@@ -34,3 +34,3 @@ "devDependencies": { | ||
"prettier": "^2.1.2", | ||
"socket.io": "^4.1.1", | ||
"socket.io": "^4.5.0", | ||
"socket.io-client": "^4.1.1", | ||
@@ -37,0 +37,0 @@ "ts-node": "^9.1.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25656
521
5
+ Addedsocket.io-adapter@2.4.0(transitive)
- Removedsocket.io-adapter@2.3.3(transitive)
Updatedsocket.io-adapter@~2.4.0