6
6
## How to use
7
7
8
8
``` js
9
- var io = require (' socket.io' )(3000 );
10
- var redis = require (' socket.io-redis' );
9
+ const io = require (' socket.io' )(3000 );
10
+ const redis = require (' socket.io-redis' );
11
11
io .adapter (redis ({ host: ' localhost' , port: 6379 }));
12
12
```
13
13
@@ -56,17 +56,17 @@ that a regular `Adapter` does not
56
56
Returns the list of client IDs connected to ` rooms ` across all nodes. See [ Namespace#clients(fn: Function )] ( https://github.com/socketio/socket.io#namespaceclientsfnfunction )
57
57
58
58
``` js
59
- io .of (' /' ).adapter .clients (function (err , clients ) {
59
+ io .of (' /' ).adapter .clients ((err , clients ) => {
60
60
console .log (clients); // an array containing all connected socket ids
61
61
});
62
62
63
- io .of (' /' ).adapter .clients ([' room1' , ' room2' ], function (err , clients ) {
63
+ io .of (' /' ).adapter .clients ([' room1' , ' room2' ], (err , clients ) => {
64
64
console .log (clients); // an array containing socket ids in 'room1' and/or 'room2'
65
65
});
66
66
67
67
// you can also use
68
68
69
- io .in (' room3' ).clients (function (err , clients ) {
69
+ io .in (' room3' ).clients ((err , clients ) => {
70
70
console .log (clients); // an array containing socket ids in 'room3'
71
71
});
72
72
```
@@ -76,7 +76,7 @@ io.in('room3').clients(function (err, clients) {
76
76
Returns the list of rooms the client with the given ID has joined (even on another node).
77
77
78
78
``` js
79
- io .of (' /' ).adapter .clientRooms (' <my-id>' , function (err , rooms ) {
79
+ io .of (' /' ).adapter .clientRooms (' <my-id>' , (err , rooms ) => {
80
80
if (err) { /* unknown id */ }
81
81
console .log (rooms); // an array containing every room a given id has joined.
82
82
});
@@ -87,7 +87,7 @@ io.of('/').adapter.clientRooms('<my-id>', function (err, rooms) {
87
87
Returns the list of all rooms.
88
88
89
89
``` js
90
- io .of (' /' ).adapter .allRooms (function (err , rooms ) {
90
+ io .of (' /' ).adapter .allRooms ((err , rooms ) => {
91
91
console .log (rooms); // an array containing all rooms (accross every node)
92
92
});
93
93
```
@@ -97,7 +97,7 @@ io.of('/').adapter.allRooms(function (err, rooms) {
97
97
Makes the socket with the given id join the room. The callback will be called once the socket has joined the room, or with an ` err ` argument if the socket was not found.
98
98
99
99
``` js
100
- io .of (' /' ).adapter .remoteJoin (' <my-id>' , ' room1' , function (err ) {
100
+ io .of (' /' ).adapter .remoteJoin (' <my-id>' , ' room1' , (err ) => {
101
101
if (err) { /* unknown id */ }
102
102
// success
103
103
});
@@ -108,7 +108,7 @@ io.of('/').adapter.remoteJoin('<my-id>', 'room1', function (err) {
108
108
Makes the socket with the given id leave the room. The callback will be called once the socket has left the room, or with an ` err ` argument if the socket was not found.
109
109
110
110
``` js
111
- io .of (' /' ).adapter .remoteLeave (' <my-id>' , ' room1' , function (err ) {
111
+ io .of (' /' ).adapter .remoteLeave (' <my-id>' , ' room1' , (err ) => {
112
112
if (err) { /* unknown id */ }
113
113
// success
114
114
});
@@ -119,7 +119,7 @@ io.of('/').adapter.remoteLeave('<my-id>', 'room1', function (err) {
119
119
Makes the socket with the given id to get disconnected. If ` close ` is set to true, it also closes the underlying socket. The callback will be called once the socket was disconnected, or with an ` err ` argument if the socket was not found.
120
120
121
121
``` js
122
- io .of (' /' ).adapter .remoteDisconnect (' <my-id>' , true , function (err ) {
122
+ io .of (' /' ).adapter .remoteDisconnect (' <my-id>' , true , (err ) => {
123
123
if (err) { /* unknown id */ }
124
124
// success
125
125
});
@@ -131,7 +131,7 @@ Sends a request to every nodes, that will respond through the `customHook` metho
131
131
132
132
``` js
133
133
// on every node
134
- io .of (' /' ).adapter .customHook = function (data , cb ) {
134
+ io .of (' /' ).adapter .customHook = (data , cb ) => {
135
135
cb (' hello ' + data);
136
136
}
137
137
@@ -147,8 +147,8 @@ Access the `pubClient` and `subClient` properties of the
147
147
Redis Adapter instance to subscribe to its ` error ` event:
148
148
149
149
``` js
150
- var redis = require (' socket.io-redis' );
151
- var adapter = redis (' localhost:6379' );
150
+ const redis = require (' socket.io-redis' );
151
+ const adapter = redis (' localhost:6379' );
152
152
adapter .pubClient .on (' error' , function (){});
153
153
adapter .subClient .on (' error' , function (){});
154
154
```
@@ -157,8 +157,8 @@ The errors emitted from `pubClient` and `subClient` will
157
157
also be forwarded to the adapter instance:
158
158
159
159
``` js
160
- var io = require (' socket.io' )(3000 );
161
- var redis = require (' socket.io-redis' );
160
+ const io = require (' socket.io' )(3000 );
161
+ const redis = require (' socket.io-redis' );
162
162
io .adapter (redis ({ host: ' localhost' , port: 6379 }));
163
163
io .of (' /' ).adapter .on (' error' , function (){});
164
164
```
@@ -170,13 +170,34 @@ that has a password, use pub/sub options instead of passing
170
170
a connection string.
171
171
172
172
``` js
173
- var redis = require (' redis' ).createClient ;
174
- var adapter = require (' socket.io-redis' );
175
- var pub = redis (port, host, { auth_pass: " pwd" });
176
- var sub = redis (port, host, { auth_pass: " pwd" });
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
177
io .adapter (adapter ({ pubClient: pub, subClient: sub }));
178
178
```
179
179
180
+ ## With [ ioredis] ( https://github.com/luin/ioredis ) client
181
+
182
+ ``` js
183
+ const io = require (' socket.io' )(3000 );
184
+ const Redis = require (' ioredis' );
185
+
186
+ const cluster = new Redis.Cluster ([
187
+ {
188
+ port: 6380 ,
189
+ host: ' 127.0.0.1'
190
+ },
191
+ {
192
+ port: 6381 ,
193
+ host: ' 127.0.0.1'
194
+ }
195
+ ]);
196
+
197
+ const adapter = require (' socket.io-redis' );
198
+ io .adapter (adapter ({ pubClient: cluster, subClient: cluster }));
199
+ ```
200
+
180
201
## Protocol
181
202
182
203
The ` socket.io-redis ` adapter broadcasts and receives messages on particularly named Redis channels. For global broadcasts the channel name is:
0 commit comments