Skip to content

Commit f630158

Browse files
authored
fix(adapter): do not skip local broadcast when publishAndReturnOffset throws (#5457)
Remove the `return` in the catch block of ClusterAdapter.broadcast() so that super.broadcast() is still called when remote publishing fails. This ensures local sockets receive the event even if the cluster publish errors out (e.g. due to a serialization error in the adapter layer). Related: #5456
1 parent 37aad11 commit f630158

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

packages/socket.io-adapter/lib/cluster-adapter.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,7 @@ export abstract class ClusterAdapter extends Adapter {
438438
});
439439
this.addOffsetIfNecessary(packet, opts, offset);
440440
} catch (e) {
441-
return debug(
442-
"[%s] error while broadcasting message: %s",
443-
this.uid,
444-
e.message,
445-
);
441+
debug("[%s] error while broadcasting message: %s", this.uid, e.message);
446442
}
447443
}
448444

packages/socket.io-adapter/test/cluster-adapter.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const NODES_COUNT = 3;
1515

1616
class EventEmitterAdapter extends ClusterAdapterWithHeartbeat {
1717
private offset = 1;
18+
public shouldFailPublish = false;
1819

1920
constructor(
2021
nsp,
@@ -27,6 +28,9 @@ class EventEmitterAdapter extends ClusterAdapterWithHeartbeat {
2728
}
2829

2930
protected doPublish(message: ClusterMessage): Promise<string> {
31+
if (this.shouldFailPublish) {
32+
return Promise.reject(new Error("publish failed"));
33+
}
3034
this.eventBus.emit("message", message);
3135
return Promise.resolve(String(++this.offset));
3236
}
@@ -152,6 +156,19 @@ describe("cluster adapter", () => {
152156
servers[0].local.emit("test");
153157
});
154158

159+
it("broadcasts to local clients even when publishAndReturnOffset throws", (done) => {
160+
const adapter = servers[0].of("/").adapter as EventEmitterAdapter;
161+
adapter.shouldFailPublish = true;
162+
163+
clientSockets[0].on("test", (arg1) => {
164+
expect(arg1).to.eql(1);
165+
adapter.shouldFailPublish = false;
166+
done();
167+
});
168+
169+
servers[0].emit("test", 1);
170+
});
171+
155172
it("broadcasts with multiple acknowledgements", (done) => {
156173
clientSockets[0].on("test", (cb) => cb(1));
157174
clientSockets[1].on("test", (cb) => cb(2));

0 commit comments

Comments
 (0)