30
30
31
31
## How to use
32
32
33
+ Installation:
34
+
35
+ ```
36
+ $ npm install @socket.io/redis-adapter redis
37
+ ```
38
+
33
39
### CommonJS
34
40
35
41
``` js
36
42
const io = require (' socket.io' )(3000 );
43
+ const { createClient } = require (' redis' );
37
44
const redisAdapter = require (' @socket.io/redis-adapter' );
38
- io .adapter (redisAdapter ({ host: ' localhost' , port: 6379 }));
45
+
46
+ const pubClient = createClient ({ host: ' localhost' , port: 6379 });
47
+ const subClient = pubClient .duplicate ();
48
+ io .adapter (redisAdapter (pubClient, subClient));
39
49
```
40
50
41
51
### ES6 modules
42
52
43
53
``` js
44
54
import { Server } from ' socket.io' ;
55
+ import { createClient } from ' redis' ;
45
56
import redisAdapter from ' @socket.io/redis-adapter' ;
46
57
47
58
const io = new Server (3000 );
48
- io .adapter (redisAdapter ({ host: ' localhost' , port: 6379 }));
59
+ const pubClient = createClient ({ host: ' localhost' , port: 6379 });
60
+ const subClient = pubClient .duplicate ();
61
+ io .adapter (redisAdapter (pubClient, subClient));
49
62
```
50
63
51
64
### TypeScript
52
65
53
66
``` ts
54
- // npm i -D @types/redis
67
+ // npm i -D redis @types/redis
55
68
import { Server } from ' socket.io' ;
56
69
import { createAdapter } from ' @socket.io/redis-adapter' ;
57
70
import { RedisClient } from ' redis' ;
@@ -60,7 +73,7 @@ const io = new Server(8080);
60
73
const pubClient = new RedisClient ({ host: ' localhost' , port: 6379 });
61
74
const subClient = pubClient .duplicate ();
62
75
63
- io .adapter (createAdapter ({ pubClient , subClient } ));
76
+ io .adapter (createAdapter (pubClient , subClient ));
64
77
```
65
78
66
79
By running Socket.IO with the ` @socket.io/redis-adapter ` adapter you can run
@@ -150,26 +163,13 @@ The request and response channels are used in the additional methods exposed by
150
163
151
164
## API
152
165
153
- ### adapter(uri[ , opts] )
154
-
155
- ` uri ` is a string like ` localhost:6379 ` where your redis server
156
- is located. For a list of options see below.
157
-
158
- ### adapter(opts)
166
+ ### adapter(pubClient, subClient[ , opts] )
159
167
160
168
The following options are allowed:
161
169
162
170
- ` key ` : the name of the key to pub/sub events on as prefix (` socket.io ` )
163
- - ` host ` : host to connect to redis on (` localhost ` )
164
- - ` port ` : port to connect to redis on (` 6379 ` )
165
- - ` pubClient ` : optional, the redis client to publish events on
166
- - ` subClient ` : optional, the redis client to subscribe to events on
167
171
- ` requestsTimeout ` : optional, after this timeout the adapter will stop waiting from responses to request (` 5000ms ` )
168
172
169
- If you decide to supply ` pubClient ` and ` subClient ` , make sure you use
170
- [ node_redis] ( https://github.com/mranney/node_redis ) as a client or one
171
- with an equivalent API.
172
-
173
173
### RedisAdapter
174
174
175
175
The redis adapter instances expose the following properties
@@ -242,41 +242,6 @@ try {
242
242
}
243
243
```
244
244
245
- ## Client error handling
246
-
247
- Access the ` pubClient ` and ` subClient ` properties of the
248
- Redis Adapter instance to subscribe to its ` error ` event:
249
-
250
- ``` js
251
- const adapter = require (' @socket.io/redis-adapter' )(' localhost:6379' );
252
- adapter .pubClient .on (' error' , function (){});
253
- adapter .subClient .on (' error' , function (){});
254
- ```
255
-
256
- The errors emitted from ` pubClient ` and ` subClient ` will
257
- also be forwarded to the adapter instance:
258
-
259
- ``` js
260
- const io = require (' socket.io' )(3000 );
261
- const redisAdapter = require (' @socket.io/redis-adapter' );
262
- io .adapter (redisAdapter ({ host: ' localhost' , port: 6379 }));
263
- io .of (' /' ).adapter .on (' error' , function (){});
264
- ```
265
-
266
- ## Custom client (eg: with authentication)
267
-
268
- If you need to create a redisAdapter to a redis instance
269
- that has a password, use pub/sub options instead of passing
270
- a connection string.
271
-
272
- ``` js
273
- const redis = require (' redis' );
274
- const redisAdapter = require (' @socket.io/redis-adapter' );
275
- const pubClient = redis .createClient (port, host, { auth_pass: " pwd" });
276
- const subClient = pubClient .duplicate ();
277
- io .adapter (redisAdapter ({ pubClient, subClient }));
278
- ```
279
-
280
245
## With ioredis client
281
246
282
247
### Cluster example
@@ -297,10 +262,10 @@ const startupNodes = [
297
262
}
298
263
];
299
264
300
- io . adapter ( redisAdapter ({
301
- pubClient : new Redis.Cluster (startupNodes),
302
- subClient : new Redis.Cluster (startupNodes)
303
- } ));
265
+ const pubClient = new Redis.Cluster (startupNodes);
266
+ const subClient = pubClient . duplicate ();
267
+
268
+ io . adapter ( redisAdapter (pubClient, subClient ));
304
269
```
305
270
306
271
### Sentinel Example
@@ -318,10 +283,10 @@ const options = {
318
283
name: ' master01'
319
284
};
320
285
321
- io . adapter ( redisAdapter ({
322
- pubClient : new Redis (options),
323
- subClient : new Redis (options)
324
- } ));
286
+ const pubClient = new Redis (options);
287
+ const subClient = pubClient . duplicate ();
288
+
289
+ io . adapter ( redisAdapter (pubClient, subClient ));
325
290
```
326
291
327
292
## Protocol
0 commit comments