Skip to content

Commit d6586a1

Browse files
authoredJun 14, 2017
[docs] Add an example with ioredis (#231)
1 parent f978d24 commit d6586a1

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed
 

‎README.md

+40-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
## How to use
77

88
```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');
1111
io.adapter(redis({ host: 'localhost', port: 6379 }));
1212
```
1313

@@ -56,17 +56,17 @@ that a regular `Adapter` does not
5656
Returns the list of client IDs connected to `rooms` across all nodes. See [Namespace#clients(fn:Function)](https://github.com/socketio/socket.io#namespaceclientsfnfunction)
5757

5858
```js
59-
io.of('/').adapter.clients(function (err, clients) {
59+
io.of('/').adapter.clients((err, clients) => {
6060
console.log(clients); // an array containing all connected socket ids
6161
});
6262

63-
io.of('/').adapter.clients(['room1', 'room2'], function (err, clients) {
63+
io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => {
6464
console.log(clients); // an array containing socket ids in 'room1' and/or 'room2'
6565
});
6666

6767
// you can also use
6868

69-
io.in('room3').clients(function (err, clients) {
69+
io.in('room3').clients((err, clients) => {
7070
console.log(clients); // an array containing socket ids in 'room3'
7171
});
7272
```
@@ -76,7 +76,7 @@ io.in('room3').clients(function (err, clients) {
7676
Returns the list of rooms the client with the given ID has joined (even on another node).
7777

7878
```js
79-
io.of('/').adapter.clientRooms('<my-id>', function (err, rooms) {
79+
io.of('/').adapter.clientRooms('<my-id>', (err, rooms) => {
8080
if (err) { /* unknown id */ }
8181
console.log(rooms); // an array containing every room a given id has joined.
8282
});
@@ -87,7 +87,7 @@ io.of('/').adapter.clientRooms('<my-id>', function (err, rooms) {
8787
Returns the list of all rooms.
8888

8989
```js
90-
io.of('/').adapter.allRooms(function (err, rooms) {
90+
io.of('/').adapter.allRooms((err, rooms) => {
9191
console.log(rooms); // an array containing all rooms (accross every node)
9292
});
9393
```
@@ -97,7 +97,7 @@ io.of('/').adapter.allRooms(function (err, rooms) {
9797
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.
9898

9999
```js
100-
io.of('/').adapter.remoteJoin('<my-id>', 'room1', function (err) {
100+
io.of('/').adapter.remoteJoin('<my-id>', 'room1', (err) => {
101101
if (err) { /* unknown id */ }
102102
// success
103103
});
@@ -108,7 +108,7 @@ io.of('/').adapter.remoteJoin('<my-id>', 'room1', function (err) {
108108
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.
109109

110110
```js
111-
io.of('/').adapter.remoteLeave('<my-id>', 'room1', function (err) {
111+
io.of('/').adapter.remoteLeave('<my-id>', 'room1', (err) => {
112112
if (err) { /* unknown id */ }
113113
// success
114114
});
@@ -119,7 +119,7 @@ io.of('/').adapter.remoteLeave('<my-id>', 'room1', function (err) {
119119
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.
120120

121121
```js
122-
io.of('/').adapter.remoteDisconnect('<my-id>', true, function (err) {
122+
io.of('/').adapter.remoteDisconnect('<my-id>', true, (err) => {
123123
if (err) { /* unknown id */ }
124124
// success
125125
});
@@ -131,7 +131,7 @@ Sends a request to every nodes, that will respond through the `customHook` metho
131131

132132
```js
133133
// on every node
134-
io.of('/').adapter.customHook = function (data, cb) {
134+
io.of('/').adapter.customHook = (data, cb) => {
135135
cb('hello ' + data);
136136
}
137137

@@ -147,8 +147,8 @@ Access the `pubClient` and `subClient` properties of the
147147
Redis Adapter instance to subscribe to its `error` event:
148148

149149
```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');
152152
adapter.pubClient.on('error', function(){});
153153
adapter.subClient.on('error', function(){});
154154
```
@@ -157,8 +157,8 @@ The errors emitted from `pubClient` and `subClient` will
157157
also be forwarded to the adapter instance:
158158

159159
```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');
162162
io.adapter(redis({ host: 'localhost', port: 6379 }));
163163
io.of('/').adapter.on('error', function(){});
164164
```
@@ -170,13 +170,34 @@ that has a password, use pub/sub options instead of passing
170170
a connection string.
171171

172172
```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" });
177177
io.adapter(adapter({ pubClient: pub, subClient: sub }));
178178
```
179179

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+
180201
## Protocol
181202

182203
The `socket.io-redis` adapter broadcasts and receives messages on particularly named Redis channels. For global broadcasts the channel name is:

0 commit comments

Comments
 (0)
Please sign in to comment.