Skip to content

Commit 8e5c84f

Browse files
feat: do not emit "error" events anymore
Previously, the "error" events from the two Redis clients were forwarded by each instance of the adapter, which made it quite hard to handle the errors with a lot of namespaces (as there is one instance of adapter per namespace). ```js io.of("/my-namespace").adapter.on("error", () => { // ... }); ``` The adapter instance will no longer emit "error" events, and will print a warning if there is no other error handler registered (for backward compatibility). The error handling must be done directly on the Redis client: ```js redisClient.on("error", (err) => { // something went wrong, maybe the connection to the server was lost }); ``` Related: - #412 - #425
1 parent f66de11 commit 8e5c84f

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

lib/index.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ export class RedisAdapter extends Adapter {
114114
this.responseChannel = prefix + "-response#" + this.nsp.name + "#";
115115
const specificResponseChannel = this.responseChannel + this.uid + "#";
116116

117-
const onError = (err) => {
118-
if (err) {
119-
this.emit("error", err);
120-
}
121-
};
122-
123117
const isRedisV4 = typeof this.pubClient.pSubscribe === "function";
124118
if (isRedisV4) {
125119
this.subClient.pSubscribe(
@@ -136,18 +130,27 @@ export class RedisAdapter extends Adapter {
136130
}
137131
);
138132
} else {
139-
this.subClient.psubscribe(this.channel + "*", onError);
133+
this.subClient.psubscribe(this.channel + "*");
140134
this.subClient.on("pmessageBuffer", this.onmessage.bind(this));
141135

142-
this.subClient.subscribe(
143-
[this.requestChannel, this.responseChannel, specificResponseChannel],
144-
onError
145-
);
136+
this.subClient.subscribe([
137+
this.requestChannel,
138+
this.responseChannel,
139+
specificResponseChannel,
140+
]);
146141
this.subClient.on("messageBuffer", this.onrequest.bind(this));
147142
}
148143

149-
this.pubClient.on("error", onError);
150-
this.subClient.on("error", onError);
144+
const registerFriendlyErrorHandler = (redisClient) => {
145+
redisClient.on("error", () => {
146+
if (redisClient.listenerCount("error") === 1) {
147+
console.warn("missing 'error' handler on this Redis client");
148+
}
149+
});
150+
};
151+
152+
registerFriendlyErrorHandler(this.pubClient);
153+
registerFriendlyErrorHandler(this.subClient);
151154
}
152155

153156
/**
@@ -211,7 +214,7 @@ export class RedisAdapter extends Adapter {
211214
try {
212215
request = JSON.parse(msg);
213216
} catch (err) {
214-
this.emit("error", err);
217+
debug("ignoring malformed request");
215218
return;
216219
}
217220

@@ -406,7 +409,7 @@ export class RedisAdapter extends Adapter {
406409
try {
407410
response = JSON.parse(msg);
408411
} catch (err) {
409-
this.emit("error", err);
412+
debug("ignoring malformed response");
410413
return;
411414
}
412415

0 commit comments

Comments
 (0)