Skip to content

Commit 91fb8ed

Browse files
committedFeb 27, 2018
[docs] Add an example with Sentinel in the README
1 parent 5fe5dad commit 91fb8ed

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed
 

‎README.md

+48-13
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@
77

88
```js
99
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 }));
1212
```
1313

1414
By running socket.io with the `socket.io-redis` adapter you can run
1515
multiple socket.io instances in different processes or servers that can
1616
all broadcast and emit events to and from each other.
1717

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+
1832
If you need to emit events to socket.io instances from a non-socket.io
1933
process, you should use [socket.io-emitter](https://github.com/socketio/socket.io-emitter).
2034

@@ -147,8 +161,7 @@ Access the `pubClient` and `subClient` properties of the
147161
Redis Adapter instance to subscribe to its `error` event:
148162

149163
```js
150-
const redis = require('socket.io-redis');
151-
const adapter = redis('localhost:6379');
164+
const adapter = require('socket.io-redis')('localhost:6379');
152165
adapter.pubClient.on('error', function(){});
153166
adapter.subClient.on('error', function(){});
154167
```
@@ -158,8 +171,8 @@ also be forwarded to the adapter instance:
158171

159172
```js
160173
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 }));
163176
io.of('/').adapter.on('error', function(){});
164177
```
165178

@@ -170,15 +183,17 @@ that has a password, use pub/sub options instead of passing
170183
a connection string.
171184

172185
```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 }));
178191
```
179192

180193
## With [ioredis](https://github.com/luin/ioredis) client
181194

195+
### Cluster example
196+
182197
```js
183198
const io = require('socket.io')(3000);
184199
const Redis = require('ioredis');
@@ -194,8 +209,28 @@ const cluster = new Redis.Cluster([
194209
}
195210
]);
196211

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 }));
199234
```
200235

201236
## Protocol

0 commit comments

Comments
 (0)
Please sign in to comment.