@@ -42,6 +42,18 @@ export interface RedisAdapterOptions {
42
42
* @default 5000
43
43
*/
44
44
requestsTimeout : number ;
45
+ /**
46
+ * Whether to publish a response to the channel specific to the requesting node.
47
+ *
48
+ * - if true, the response will be published to `${key}-request#${nsp}#${uid}#`
49
+ * - if false, the response will be published to `${key}-request#${nsp}#`
50
+ *
51
+ * This option currently defaults to false for backward compatibility, but will be set to true in the next major
52
+ * release.
53
+ *
54
+ * @default false
55
+ */
56
+ publishOnSpecificResponseChannel : boolean ;
45
57
}
46
58
47
59
/**
@@ -66,6 +78,7 @@ export function createAdapter(
66
78
export class RedisAdapter extends Adapter {
67
79
public readonly uid ;
68
80
public readonly requestsTimeout : number ;
81
+ public readonly publishOnSpecificResponseChannel : boolean ;
69
82
70
83
private readonly channel : string ;
71
84
private readonly requestChannel : string ;
@@ -92,12 +105,14 @@ export class RedisAdapter extends Adapter {
92
105
93
106
this . uid = uid2 ( 6 ) ;
94
107
this . requestsTimeout = opts . requestsTimeout || 5000 ;
108
+ this . publishOnSpecificResponseChannel = ! ! opts . publishOnSpecificResponseChannel ;
95
109
96
110
const prefix = opts . key || "socket.io" ;
97
111
98
112
this . channel = prefix + "#" + nsp . name + "#" ;
99
113
this . requestChannel = prefix + "-request#" + this . nsp . name + "#" ;
100
114
this . responseChannel = prefix + "-response#" + this . nsp . name + "#" ;
115
+ const specificResponseChannel = this . responseChannel + this . uid + "#" ;
101
116
102
117
const onError = ( err ) => {
103
118
if ( err ) {
@@ -115,7 +130,7 @@ export class RedisAdapter extends Adapter {
115
130
true
116
131
) ;
117
132
this . subClient . subscribe (
118
- [ this . requestChannel , this . responseChannel ] ,
133
+ [ this . requestChannel , this . responseChannel , specificResponseChannel ] ,
119
134
( msg , channel ) => {
120
135
this . onrequest ( channel , msg ) ;
121
136
}
@@ -125,7 +140,7 @@ export class RedisAdapter extends Adapter {
125
140
this . subClient . on ( "pmessageBuffer" , this . onmessage . bind ( this ) ) ;
126
141
127
142
this . subClient . subscribe (
128
- [ this . requestChannel , this . responseChannel ] ,
143
+ [ this . requestChannel , this . responseChannel , specificResponseChannel ] ,
129
144
onError
130
145
) ;
131
146
this . subClient . on ( "messageBuffer" , this . onrequest . bind ( this ) ) ;
@@ -217,7 +232,7 @@ export class RedisAdapter extends Adapter {
217
232
sockets : [ ...sockets ] ,
218
233
} ) ;
219
234
220
- this . pubClient . publish ( this . responseChannel , response ) ;
235
+ this . publishResponse ( request , response ) ;
221
236
break ;
222
237
223
238
case RequestType . ALL_ROOMS :
@@ -230,7 +245,7 @@ export class RedisAdapter extends Adapter {
230
245
rooms : [ ...this . rooms . keys ( ) ] ,
231
246
} ) ;
232
247
233
- this . pubClient . publish ( this . responseChannel , response ) ;
248
+ this . publishResponse ( request , response ) ;
234
249
break ;
235
250
236
251
case RequestType . REMOTE_JOIN :
@@ -253,7 +268,7 @@ export class RedisAdapter extends Adapter {
253
268
requestId : request . requestId ,
254
269
} ) ;
255
270
256
- this . pubClient . publish ( this . responseChannel , response ) ;
271
+ this . publishResponse ( request , response ) ;
257
272
break ;
258
273
259
274
case RequestType . REMOTE_LEAVE :
@@ -276,7 +291,7 @@ export class RedisAdapter extends Adapter {
276
291
requestId : request . requestId ,
277
292
} ) ;
278
293
279
- this . pubClient . publish ( this . responseChannel , response ) ;
294
+ this . publishResponse ( request , response ) ;
280
295
break ;
281
296
282
297
case RequestType . REMOTE_DISCONNECT :
@@ -299,7 +314,7 @@ export class RedisAdapter extends Adapter {
299
314
requestId : request . requestId ,
300
315
} ) ;
301
316
302
- this . pubClient . publish ( this . responseChannel , response ) ;
317
+ this . publishResponse ( request , response ) ;
303
318
break ;
304
319
305
320
case RequestType . REMOTE_FETCH :
@@ -327,7 +342,7 @@ export class RedisAdapter extends Adapter {
327
342
} ) ,
328
343
} ) ;
329
344
330
- this . pubClient . publish ( this . responseChannel , response ) ;
345
+ this . publishResponse ( request , response ) ;
331
346
break ;
332
347
333
348
case RequestType . SERVER_SIDE_EMIT :
@@ -366,6 +381,20 @@ export class RedisAdapter extends Adapter {
366
381
}
367
382
}
368
383
384
+ /**
385
+ * Send the response to the requesting node
386
+ * @param request
387
+ * @param response
388
+ * @private
389
+ */
390
+ private publishResponse ( request , response ) {
391
+ const responseChannel = this . publishOnSpecificResponseChannel
392
+ ? `${ this . responseChannel } ${ request . uid } #`
393
+ : this . responseChannel ;
394
+ debug ( "publishing response to channel %s" , responseChannel ) ;
395
+ this . pubClient . publish ( responseChannel , response ) ;
396
+ }
397
+
369
398
/**
370
399
* Called on response from another node
371
400
*
@@ -510,6 +539,7 @@ export class RedisAdapter extends Adapter {
510
539
511
540
const requestId = uid2 ( 6 ) ;
512
541
const request = JSON . stringify ( {
542
+ uid : this . uid ,
513
543
requestId,
514
544
type : RequestType . SOCKETS ,
515
545
rooms : [ ...rooms ] ,
@@ -554,6 +584,7 @@ export class RedisAdapter extends Adapter {
554
584
555
585
const requestId = uid2 ( 6 ) ;
556
586
const request = JSON . stringify ( {
587
+ uid : this . uid ,
557
588
requestId,
558
589
type : RequestType . ALL_ROOMS ,
559
590
} ) ;
@@ -598,6 +629,7 @@ export class RedisAdapter extends Adapter {
598
629
}
599
630
600
631
const request = JSON . stringify ( {
632
+ uid : this . uid ,
601
633
requestId,
602
634
type : RequestType . REMOTE_JOIN ,
603
635
sid : id ,
@@ -641,6 +673,7 @@ export class RedisAdapter extends Adapter {
641
673
}
642
674
643
675
const request = JSON . stringify ( {
676
+ uid : this . uid ,
644
677
requestId,
645
678
type : RequestType . REMOTE_LEAVE ,
646
679
sid : id ,
@@ -684,6 +717,7 @@ export class RedisAdapter extends Adapter {
684
717
}
685
718
686
719
const request = JSON . stringify ( {
720
+ uid : this . uid ,
687
721
requestId,
688
722
type : RequestType . REMOTE_DISCONNECT ,
689
723
sid : id ,
@@ -729,6 +763,7 @@ export class RedisAdapter extends Adapter {
729
763
const requestId = uid2 ( 6 ) ;
730
764
731
765
const request = JSON . stringify ( {
766
+ uid : this . uid ,
732
767
requestId,
733
768
type : RequestType . REMOTE_FETCH ,
734
769
opts : {
@@ -766,6 +801,7 @@ export class RedisAdapter extends Adapter {
766
801
}
767
802
768
803
const request = JSON . stringify ( {
804
+ uid : this . uid ,
769
805
type : RequestType . REMOTE_JOIN ,
770
806
opts : {
771
807
rooms : [ ...opts . rooms ] ,
@@ -783,6 +819,7 @@ export class RedisAdapter extends Adapter {
783
819
}
784
820
785
821
const request = JSON . stringify ( {
822
+ uid : this . uid ,
786
823
type : RequestType . REMOTE_LEAVE ,
787
824
opts : {
788
825
rooms : [ ...opts . rooms ] ,
@@ -800,6 +837,7 @@ export class RedisAdapter extends Adapter {
800
837
}
801
838
802
839
const request = JSON . stringify ( {
840
+ uid : this . uid ,
803
841
type : RequestType . REMOTE_DISCONNECT ,
804
842
opts : {
805
843
rooms : [ ...opts . rooms ] ,
0 commit comments