Skip to content

Commit

Permalink
fix(sharded): allow to target a specific socket ID in dynamic mode (#525
Browse files Browse the repository at this point in the history
)

Related: #524
  • Loading branch information
MartinKolarik committed Mar 13, 2024
1 parent 2113e8d commit cca38dc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/sharded-adapter.ts
Expand Up @@ -5,6 +5,10 @@ import debugModule from "debug";

const debug = debugModule("socket.io-redis");

function looksLikeASocketId(room: any) {
return typeof room === "string" && room.length === 20;
}

export interface ShardedRedisAdapterOptions {
/**
* The prefix for the Redis Pub/Sub channels.
Expand Down Expand Up @@ -128,7 +132,8 @@ class ShardedRedisAdapter extends ClusterAdapter {
this.opts.subscriptionMode === "dynamic" &&
message.type === MessageType.BROADCAST &&
message.data.requestId === undefined &&
message.data.opts.rooms.length === 1;
message.data.opts.rooms.length === 1 &&
!looksLikeASocketId(message.data.opts.rooms[0]);
if (useDynamicChannel) {
return this.dynamicChannel(message.data.opts.rooms[0]);
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/index.ts
Expand Up @@ -110,6 +110,14 @@ export function testSuite(createAdapter: any) {
servers[0].local.emit("test");
});

it("broadcasts to a single client", (done) => {
clientSockets[0].on("test", shouldNotHappen(done));
clientSockets[1].on("test", () => done());
clientSockets[2].on("test", shouldNotHappen(done));

servers[0].to(serverSockets[1].id).emit("test");
});

it("broadcasts with multiple acknowledgements", (done) => {
clientSockets[0].on("test", (cb) => cb(1));
clientSockets[1].on("test", (cb) => cb(2));
Expand Down

0 comments on commit cca38dc

Please sign in to comment.