diff --git a/lib/index.ts b/lib/index.ts index ee7cc7c..b326eb0 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -137,13 +137,25 @@ export class RedisAdapter extends Adapter { const isRedisV4 = typeof this.pubClient.pSubscribe === "function"; if (isRedisV4) { - this.subClient.pSubscribe( - this.channel + "*", + this.subClient.subscribe( + [this.channel], (msg, channel) => { this.onmessage(null, channel, msg); }, true ); + this.on("create-room", (room) => { + this.subClient.subscribe( + this.channel + room + "#", + (msg, channel) => { + this.onmessage(null, channel, msg); + }, + true + ); + }); + this.on("delete-room", (room) => { + this.subClient.unsubscribe(this.channel + room + "#"); + }); this.subClient.subscribe( [this.requestChannel, this.responseChannel, specificResponseChannel], (msg, channel) => { @@ -152,15 +164,25 @@ export class RedisAdapter extends Adapter { true ); } else { - this.subClient.psubscribe(this.channel + "*"); - this.subClient.on("pmessageBuffer", this.onmessage.bind(this)); + this.subClient.subscribe(this.channel); + this.on("create-room", (room) => { + this.subClient.subscribe(this.channel + room + "#"); + }); + this.on("delete-room", (room) => { + this.subClient.unsubscribe(this.channel + room + "#"); + }); this.subClient.subscribe([ this.requestChannel, this.responseChannel, specificResponseChannel, ]); - this.subClient.on("messageBuffer", this.onrequest.bind(this)); + this.subClient.on("messageBuffer", async (channel, msg) => { + if (channel.toString().startsWith(this.channel)) { + return this.onmessage(null, channel, msg); + } + return await this.onrequest(channel, msg); + }); } const registerFriendlyErrorHandler = (redisClient) => { @@ -185,7 +207,7 @@ export class RedisAdapter extends Adapter { const channelMatches = channel.startsWith(this.channel); if (!channelMatches) { - return debug("ignore different channel"); + return debug(`ignore different channel (onmessage): ${channel}`); } const room = channel.slice(this.channel.length, -1); @@ -228,7 +250,7 @@ export class RedisAdapter extends Adapter { if (channel.startsWith(this.responseChannel)) { return this.onresponse(channel, msg); } else if (!channel.startsWith(this.requestChannel)) { - return debug("ignore different channel"); + return debug(`ignore different channel (onrequest): ${channel}`); } let request;