@@ -416,6 +416,41 @@ function adapter(uri, opts) {
416
416
Adapter . prototype . broadcast . call ( this , packet , opts ) ;
417
417
} ;
418
418
419
+
420
+ /**
421
+ * Get the number of subscribers of a channel
422
+ *
423
+ * @param {String } channel
424
+ */
425
+
426
+ function getNumSub ( channel ) {
427
+ if ( pub . constructor . name != 'Cluster' ) {
428
+ // RedisClient or Redis
429
+ return new Promise ( function ( resolve , reject ) {
430
+ pub . send_command ( 'pubsub' , [ 'numsub' , channel ] , function ( err , numsub ) {
431
+ if ( err ) return reject ( err ) ;
432
+ resolve ( parseInt ( numsub [ 1 ] , 10 ) ) ;
433
+ } ) ;
434
+ } )
435
+ } else {
436
+ // Cluster
437
+ var nodes = pub . nodes ( ) ;
438
+ return Promise . all (
439
+ nodes . map ( function ( node ) {
440
+ return node . send_command ( 'pubsub' , [ 'numsub' , channel ] ) ;
441
+ } )
442
+ ) . then ( function ( values ) {
443
+ var numsub = 0 ;
444
+ values . map ( function ( value ) {
445
+ numsub += parseInt ( value [ 1 ] , 10 ) ;
446
+ } )
447
+ return numsub ;
448
+ } ) . catch ( function ( err ) {
449
+ throw err ;
450
+ } ) ;
451
+ }
452
+ }
453
+
419
454
/**
420
455
* Gets a list of clients by sid.
421
456
*
@@ -435,14 +470,7 @@ function adapter(uri, opts) {
435
470
var self = this ;
436
471
var requestid = uid2 ( 6 ) ;
437
472
438
- pub . send_command ( 'pubsub' , [ 'numsub' , self . requestChannel ] , function ( err , numsub ) {
439
- if ( err ) {
440
- self . emit ( 'error' , err ) ;
441
- if ( fn ) fn ( err ) ;
442
- return ;
443
- }
444
-
445
- numsub = parseInt ( numsub [ 1 ] , 10 ) ;
473
+ getNumSub ( self . requestChannel ) . then ( numsub => {
446
474
debug ( 'waiting for %d responses to "clients" request' , numsub ) ;
447
475
448
476
var request = JSON . stringify ( {
@@ -468,6 +496,9 @@ function adapter(uri, opts) {
468
496
} ;
469
497
470
498
pub . publish ( self . requestChannel , request ) ;
499
+ } ) . catch ( err => {
500
+ self . emit ( 'error' , err ) ;
501
+ if ( fn ) fn ( err ) ;
471
502
} ) ;
472
503
} ;
473
504
@@ -524,14 +555,7 @@ function adapter(uri, opts) {
524
555
var self = this ;
525
556
var requestid = uid2 ( 6 ) ;
526
557
527
- pub . send_command ( 'pubsub' , [ 'numsub' , self . requestChannel ] , function ( err , numsub ) {
528
- if ( err ) {
529
- self . emit ( 'error' , err ) ;
530
- if ( fn ) fn ( err ) ;
531
- return ;
532
- }
533
-
534
- numsub = parseInt ( numsub [ 1 ] , 10 ) ;
558
+ getNumSub ( self . requestChannel ) . then ( numsub => {
535
559
debug ( 'waiting for %d responses to "allRooms" request' , numsub ) ;
536
560
537
561
var request = JSON . stringify ( {
@@ -556,6 +580,9 @@ function adapter(uri, opts) {
556
580
} ;
557
581
558
582
pub . publish ( self . requestChannel , request ) ;
583
+ } ) . catch ( err => {
584
+ self . emit ( 'error' , err ) ;
585
+ if ( fn ) fn ( err ) ;
559
586
} ) ;
560
587
} ;
561
588
@@ -700,14 +727,7 @@ function adapter(uri, opts) {
700
727
var self = this ;
701
728
var requestid = uid2 ( 6 ) ;
702
729
703
- pub . send_command ( 'pubsub' , [ 'numsub' , self . requestChannel ] , function ( err , numsub ) {
704
- if ( err ) {
705
- self . emit ( 'error' , err ) ;
706
- if ( fn ) fn ( err ) ;
707
- return ;
708
- }
709
-
710
- numsub = parseInt ( numsub [ 1 ] , 10 ) ;
730
+ getNumSub ( self . requestChannel ) . then ( numsub => {
711
731
debug ( 'waiting for %d responses to "customRequest" request' , numsub ) ;
712
732
713
733
var request = JSON . stringify ( {
@@ -733,6 +753,9 @@ function adapter(uri, opts) {
733
753
} ;
734
754
735
755
pub . publish ( self . requestChannel , request ) ;
756
+ } ) . catch ( err => {
757
+ self . emit ( 'error' , err ) ;
758
+ if ( fn ) fn ( err ) ;
736
759
} ) ;
737
760
} ;
738
761
0 commit comments