1
1
import { ClusterAdapter , ClusterMessage , MessageType } from "./cluster-adapter" ;
2
2
import { decode , encode } from "notepack.io" ;
3
- import { hasBinary , parseNumSubResponse , sumValues } from "./util" ;
3
+ import { hasBinary , PUBSUB , SPUBLISH , SSUBSCRIBE , SUNSUBSCRIBE } from "./util" ;
4
4
import debugModule from "debug" ;
5
5
6
6
const debug = debugModule ( "socket.io-redis" ) ;
7
7
8
- const RETURN_BUFFERS = true ;
9
-
10
8
export interface ShardedRedisAdapterOptions {
11
9
/**
12
10
* The prefix for the Redis Pub/Sub channels.
@@ -78,25 +76,21 @@ class ShardedRedisAdapter extends ClusterAdapter {
78
76
79
77
const handler = ( message , channel ) => this . onRawMessage ( message , channel ) ;
80
78
81
- this . subClient . sSubscribe ( this . channel , handler , RETURN_BUFFERS ) ;
82
- this . subClient . sSubscribe ( this . responseChannel , handler , RETURN_BUFFERS ) ;
79
+ SSUBSCRIBE ( this . subClient , this . channel , handler ) ;
80
+ SSUBSCRIBE ( this . subClient , this . responseChannel , handler ) ;
83
81
84
82
if ( this . opts . subscriptionMode === "dynamic" ) {
85
83
this . on ( "create-room" , ( room ) => {
86
84
const isPublicRoom = ! this . sids . has ( room ) ;
87
85
if ( isPublicRoom ) {
88
- this . subClient . sSubscribe (
89
- this . dynamicChannel ( room ) ,
90
- handler ,
91
- RETURN_BUFFERS
92
- ) ;
86
+ SSUBSCRIBE ( this . subClient , this . dynamicChannel ( room ) , handler ) ;
93
87
}
94
88
} ) ;
95
89
96
90
this . on ( "delete-room" , ( room ) => {
97
91
const isPublicRoom = ! this . sids . has ( room ) ;
98
92
if ( isPublicRoom ) {
99
- this . subClient . sUnsubscribe ( this . dynamicChannel ( room ) ) ;
93
+ SUNSUBSCRIBE ( this . subClient , this . dynamicChannel ( room ) ) ;
100
94
}
101
95
} ) ;
102
96
}
@@ -114,13 +108,13 @@ class ShardedRedisAdapter extends ClusterAdapter {
114
108
} ) ;
115
109
}
116
110
117
- return this . subClient . sUnsubscribe ( channels ) ;
111
+ return SUNSUBSCRIBE ( this . subClient , channels ) ;
118
112
}
119
113
120
114
override publishMessage ( message ) {
121
115
const channel = this . computeChannel ( message ) ;
122
116
debug ( "publishing message of type %s to %s" , message . type , channel ) ;
123
- this . pubClient . sPublish ( channel , this . encode ( message ) ) ;
117
+ SPUBLISH ( this . pubClient , channel , this . encode ( message ) ) ;
124
118
125
119
return Promise . resolve ( "" ) ;
126
120
}
@@ -147,7 +141,8 @@ class ShardedRedisAdapter extends ClusterAdapter {
147
141
override publishResponse ( requesterUid , response ) {
148
142
debug ( "publishing response of type %s to %s" , response . type , requesterUid ) ;
149
143
150
- this . pubClient . sPublish (
144
+ SPUBLISH (
145
+ this . pubClient ,
151
146
`${ this . channel } ${ requesterUid } #` ,
152
147
this . encode ( response )
153
148
) ;
@@ -189,21 +184,6 @@ class ShardedRedisAdapter extends ClusterAdapter {
189
184
}
190
185
191
186
override serverCount ( ) : Promise < number > {
192
- if (
193
- this . pubClient . constructor . name === "Cluster" ||
194
- this . pubClient . isCluster
195
- ) {
196
- return Promise . all (
197
- this . pubClient . nodes ( ) . map ( ( node ) => {
198
- return node
199
- . sendCommand ( [ "PUBSUB" , "SHARDNUMSUB" , this . channel ] )
200
- . then ( parseNumSubResponse ) ;
201
- } )
202
- ) . then ( sumValues ) ;
203
- } else {
204
- return this . pubClient
205
- . sendCommand ( [ "PUBSUB" , "SHARDNUMSUB" , this . channel ] )
206
- . then ( parseNumSubResponse ) ;
207
- }
187
+ return PUBSUB ( this . pubClient , "SHARDNUMSUB" , this . channel ) ;
208
188
}
209
189
}
0 commit comments