Skip to content

Commit 39d4220

Browse files
refactor(cluster): catch publish errors
Note: the current API does not currently allow the user to handle those errors (and retry, for example). This might be worth investigating for the next major version, maybe something like: ```js try { await io.emit("hello"); } catch (e) { // something went wrong } ``` Related: socketio/socket.io-mongo-adapter#15
1 parent 1011ab3 commit 39d4220

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

lib/cluster-adapter.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export abstract class ClusterAdapter extends Adapter {
390390

391391
if (!onlyLocal) {
392392
try {
393-
const offset = await this.publish({
393+
const offset = await this.publishAndReturnOffset({
394394
type: MessageType.BROADCAST,
395395
data: {
396396
packet,
@@ -623,7 +623,17 @@ export abstract class ClusterAdapter extends Adapter {
623623
});
624624
}
625625

626-
protected publish(message: DistributiveOmit<ClusterMessage, "nsp" | "uid">) {
626+
protected publish(
627+
message: DistributiveOmit<ClusterMessage, "nsp" | "uid">
628+
): void {
629+
this.publishAndReturnOffset(message).catch((err) => {
630+
debug("error while publishing message: %s", err);
631+
});
632+
}
633+
634+
protected publishAndReturnOffset(
635+
message: DistributiveOmit<ClusterMessage, "nsp" | "uid">
636+
) {
627637
(message as ClusterMessage).uid = this.uid;
628638
(message as ClusterMessage).nsp = this.nsp.name;
629639
return this.doPublish(message as ClusterMessage);
@@ -644,7 +654,11 @@ export abstract class ClusterAdapter extends Adapter {
644654
) {
645655
(response as ClusterResponse).uid = this.uid;
646656
(response as ClusterResponse).nsp = this.nsp.name;
647-
return this.doPublishResponse(requesterUid, response as ClusterResponse);
657+
this.doPublishResponse(requesterUid, response as ClusterResponse).catch(
658+
(err) => {
659+
debug("error while publishing response: %s", err);
660+
}
661+
);
648662
}
649663

650664
/**
@@ -657,7 +671,7 @@ export abstract class ClusterAdapter extends Adapter {
657671
protected abstract doPublishResponse(
658672
requesterUid: string,
659673
response: ClusterResponse
660-
);
674+
): Promise<void>;
661675
}
662676

663677
interface CustomClusterRequest {

0 commit comments

Comments
 (0)