3030
3131## How to use
3232
33+ Installation:
34+
35+ ```
36+ $ npm install @socket.io/redis-adapter redis
37+ ```
38+
3339### CommonJS
3440
3541``` js
3642const io = require (' socket.io' )(3000 );
43+ const { createClient } = require (' redis' );
3744const 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));
3949```
4050
4151### ES6 modules
4252
4353``` js
4454import { Server } from ' socket.io' ;
55+ import { createClient } from ' redis' ;
4556import redisAdapter from ' @socket.io/redis-adapter' ;
4657
4758const 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));
4962```
5063
5164### TypeScript
5265
5366``` ts
54- // npm i -D @types/redis
67+ // npm i -D redis @types/redis
5568import { Server } from ' socket.io' ;
5669import { createAdapter } from ' @socket.io/redis-adapter' ;
5770import { RedisClient } from ' redis' ;
@@ -60,7 +73,7 @@ const io = new Server(8080);
6073const pubClient = new RedisClient ({ host: ' localhost' , port: 6379 });
6174const subClient = pubClient .duplicate ();
6275
63- io .adapter (createAdapter ({ pubClient , subClient } ));
76+ io .adapter (createAdapter (pubClient , subClient ));
6477```
6578
6679By 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
150163
151164## API
152165
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] )
159167
160168The following options are allowed:
161169
162170- ` 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
167171- ` requestsTimeout ` : optional, after this timeout the adapter will stop waiting from responses to request (` 5000ms ` )
168172
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-
173173### RedisAdapter
174174
175175The redis adapter instances expose the following properties
@@ -242,41 +242,6 @@ try {
242242}
243243```
244244
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-
280245## With ioredis client
281246
282247### Cluster example
@@ -297,10 +262,10 @@ const startupNodes = [
297262 }
298263];
299264
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 ));
304269```
305270
306271### Sentinel Example
@@ -318,10 +283,10 @@ const options = {
318283 name: ' master01'
319284};
320285
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 ));
325290```
326291
327292## Protocol
0 commit comments