Skip to content

Commit

Permalink
[feature] Make subEvent default to messageBuffer (#157)
Browse files Browse the repository at this point in the history
With that subscription event the `return_buffers` flag is not needed
anymore.
  • Loading branch information
darrachequesne committed Dec 8, 2016
1 parent aae6f21 commit 73ce3ea
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
12 changes: 2 additions & 10 deletions README.md
Expand Up @@ -32,7 +32,7 @@ The following options are allowed:
- `key`: the name of the key to pub/sub events on as prefix (`socket.io`)
- `host`: host to connect to redis on (`localhost`)
- `port`: port to connect to redis on (`6379`)
- `subEvent`: optional, the redis client event name to subscribe to (`message`)
- `subEvent`: optional, the redis client event name to subscribe to (`messageBuffer`)
- `pubClient`: optional, the redis client to publish events on
- `subClient`: optional, the redis client to subscribe to events on
- `requestsTimeout`: optional, after this timeout the adapter will stop waiting from responses to request (`1000ms`)
Expand All @@ -42,12 +42,6 @@ If you decide to supply `pubClient` and `subClient`, make sure you use
[node_redis](https://github.com/mranney/node_redis) as a client or one
with an equivalent API.

If you supply clients, make sure you initialized them with
the `return_buffers` option set to `true`.

You can supply [ioredis](https://github.com/luin/ioredis) as a client
by providing `messageBuffer` as the subEvent option.

### RedisAdapter

The redis adapter instances expose the following properties
Expand Down Expand Up @@ -89,12 +83,10 @@ a connection string.
var redis = require('redis').createClient;
var adapter = require('socket.io-redis');
var pub = redis(port, host, { auth_pass: "pwd" });
var sub = redis(port, host, { return_buffers: true, auth_pass: "pwd" });
var sub = redis(port, host, { auth_pass: "pwd" });
io.adapter(adapter({ pubClient: pub, subClient: sub }));
```

Make sure the `return_buffers` option is set to `true` for the sub client.

## Protocol

The `socket.io-redis` adapter broadcasts and receives messages on particularly named Redis channels. For global broadcasts the channel name is:
Expand Down
10 changes: 5 additions & 5 deletions index.js
Expand Up @@ -47,22 +47,22 @@ function adapter(uri, opts){
var sub = opts.subClient;

var prefix = opts.key || 'socket.io';
var subEvent = opts.subEvent || 'message';
var subEvent = opts.subEvent || 'messageBuffer';
var requestsTimeout = opts.requestsTimeout || 1000;
var withChannelMultiplexing = false !== opts.withChannelMultiplexing;

// init clients if needed
function createClient(redis_opts) {
function createClient() {
if (uri) {
// handle uri string
return redis(uri, redis_opts);
return redis(uri, opts);
} else {
return redis(opts.port, opts.host, redis_opts);
return redis(opts);
}
}

if (!pub) pub = createClient();
if (!sub) sub = createClient({ return_buffers: true });
if (!sub) sub = createClient();

// this server's key
var uid = uid2(6);
Expand Down
11 changes: 2 additions & 9 deletions test/index.js
Expand Up @@ -12,10 +12,7 @@ var adapter = require('../');
var redis = require('redis').createClient;
var srv = http();
var sio = io(srv);
sio.adapter(adapter({
pubClient: redis(),
subClient: redis(null, null, { return_buffers: true })
}));
sio.adapter(adapter());
srv.listen(function(err){
if (err) throw err; // abort tests
if ('function' == typeof nsp) {
Expand All @@ -32,12 +29,9 @@ var adapter = require('../');
{
name: 'socket.io-redis without channel multiplexing',
create: function create(nsp, fn){
var redis = require('redis').createClient;
var srv = http();
var sio = io(srv);
sio.adapter(adapter({
pubClient: redis(),
subClient: redis(null, null, { return_buffers: true }),
withChannelMultiplexing: false
}));
srv.listen(function(err){
Expand All @@ -61,8 +55,7 @@ var adapter = require('../');
var sio = io(srv);
sio.adapter(adapter({
pubClient: redis(),
subClient: redis(null, null, { return_buffers: true }),
subEvent: 'messageBuffer'
subClient: redis(),
}));
srv.listen(function(err){
if (err) throw err; // abort tests
Expand Down

0 comments on commit 73ce3ea

Please sign in to comment.