Skip to content

Commit cca38dc

Browse files
fix(sharded): allow to target a specific socket ID in dynamic mode (#525)
Related: #524
1 parent 2113e8d commit cca38dc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/sharded-adapter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import debugModule from "debug";
55

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

8+
function looksLikeASocketId(room: any) {
9+
return typeof room === "string" && room.length === 20;
10+
}
11+
812
export interface ShardedRedisAdapterOptions {
913
/**
1014
* The prefix for the Redis Pub/Sub channels.
@@ -128,7 +132,8 @@ class ShardedRedisAdapter extends ClusterAdapter {
128132
this.opts.subscriptionMode === "dynamic" &&
129133
message.type === MessageType.BROADCAST &&
130134
message.data.requestId === undefined &&
131-
message.data.opts.rooms.length === 1;
135+
message.data.opts.rooms.length === 1 &&
136+
!looksLikeASocketId(message.data.opts.rooms[0]);
132137
if (useDynamicChannel) {
133138
return this.dynamicChannel(message.data.opts.rooms[0]);
134139
} else {

test/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ export function testSuite(createAdapter: any) {
110110
servers[0].local.emit("test");
111111
});
112112

113+
it("broadcasts to a single client", (done) => {
114+
clientSockets[0].on("test", shouldNotHappen(done));
115+
clientSockets[1].on("test", () => done());
116+
clientSockets[2].on("test", shouldNotHappen(done));
117+
118+
servers[0].to(serverSockets[1].id).emit("test");
119+
});
120+
113121
it("broadcasts with multiple acknowledgements", (done) => {
114122
clientSockets[0].on("test", (cb) => cb(1));
115123
clientSockets[1].on("test", (cb) => cb(2));

0 commit comments

Comments
 (0)