7
7
8
8
``` js
9
9
const io = require (' socket.io' )(3000 );
10
- const redis = require (' socket.io-redis' );
11
- io .adapter (redis ({ host: ' localhost' , port: 6379 }));
10
+ const redisAdapter = require (' socket.io-redis' );
11
+ io .adapter (redisAdapter ({ host: ' localhost' , port: 6379 }));
12
12
```
13
13
14
14
By running socket.io with the ` socket.io-redis ` adapter you can run
15
15
multiple socket.io instances in different processes or servers that can
16
16
all broadcast and emit events to and from each other.
17
17
18
+ So any of the following commands:
19
+
20
+ ``` js
21
+ io .emit (' hello' , ' to all clients' );
22
+ io .to (' room42' ).emit (' hello' , " to all clients in 'room42' room" );
23
+
24
+ io .on (' connection' , (socket ) => {
25
+ socket .broadcast .emit (' hello' , ' to all clients except sender' );
26
+ socket .to (' room42' ).emit (' hello' , " to all clients in 'room42' room except sender" );
27
+ });
28
+ ```
29
+
30
+ will properly be broadcast to the clients through the Redis Pub/Sub mechanism.
31
+
18
32
If you need to emit events to socket.io instances from a non-socket.io
19
33
process, you should use [ socket.io-emitter] ( https://github.com/socketio/socket.io-emitter ) .
20
34
@@ -147,8 +161,7 @@ Access the `pubClient` and `subClient` properties of the
147
161
Redis Adapter instance to subscribe to its ` error ` event:
148
162
149
163
``` js
150
- const redis = require (' socket.io-redis' );
151
- const adapter = redis (' localhost:6379' );
164
+ const adapter = require (' socket.io-redis' )(' localhost:6379' );
152
165
adapter .pubClient .on (' error' , function (){});
153
166
adapter .subClient .on (' error' , function (){});
154
167
```
@@ -158,8 +171,8 @@ also be forwarded to the adapter instance:
158
171
159
172
``` js
160
173
const io = require (' socket.io' )(3000 );
161
- const redis = require (' socket.io-redis' );
162
- io .adapter (redis ({ host: ' localhost' , port: 6379 }));
174
+ const redisAdapter = require (' socket.io-redis' );
175
+ io .adapter (redisAdapter ({ host: ' localhost' , port: 6379 }));
163
176
io .of (' /' ).adapter .on (' error' , function (){});
164
177
```
165
178
@@ -170,15 +183,17 @@ that has a password, use pub/sub options instead of passing
170
183
a connection string.
171
184
172
185
``` js
173
- const redis = require (' redis' ). createClient ;
174
- const adapter = require (' socket.io-redis' );
175
- const pub = redis (port, host, { auth_pass: " pwd" });
176
- const sub = redis (port, host, { auth_pass: " pwd" });
177
- io .adapter (adapter ({ pubClient: pub, subClient: sub }));
186
+ const redis = require (' redis' );
187
+ const redisAdapter = require (' socket.io-redis' );
188
+ const pub = redis . createClient (port, host, { auth_pass: " pwd" });
189
+ const sub = redis . createClient (port, host, { auth_pass: " pwd" });
190
+ io .adapter (redisAdapter ({ pubClient: pub, subClient: sub }));
178
191
```
179
192
180
193
## With [ ioredis] ( https://github.com/luin/ioredis ) client
181
194
195
+ ### Cluster example
196
+
182
197
``` js
183
198
const io = require (' socket.io' )(3000 );
184
199
const Redis = require (' ioredis' );
@@ -194,8 +209,28 @@ const cluster = new Redis.Cluster([
194
209
}
195
210
]);
196
211
197
- const adapter = require (' socket.io-redis' );
198
- io .adapter (adapter ({ pubClient: cluster, subClient: cluster }));
212
+ const redisAdapter = require (' socket.io-redis' );
213
+ io .adapter (redisAdapter ({ pubClient: cluster, subClient: cluster }));
214
+ ```
215
+
216
+ ### Sentinel Example
217
+
218
+ ``` js
219
+ const io = require (' socket.io' )(3000 );
220
+ const Redis = require (' ioredis' );
221
+
222
+ const options = {
223
+ sentinels: [
224
+ { host: ' somehost1' , port: 26379 },
225
+ { host: ' somehost2' , port: 26379 }
226
+ ],
227
+ name: ' master01'
228
+ };
229
+
230
+ const pubClient = new Redis (options);
231
+ const subClient = new Redis (options);
232
+
233
+ io .adapter (redisAdapter ({ pubClient: options, subClient: options }));
199
234
```
200
235
201
236
## Protocol
0 commit comments