Skip to content

Commit

Permalink
Fix pub sub mode
Browse files Browse the repository at this point in the history
There is likely a better and more performant way to fix this but this works so far
and should be good enough to release and improve later.

Make test more robust

Add another test
  • Loading branch information
Ruben Bridgewater committed Mar 26, 2016
1 parent 5294917 commit 7a5a4aa
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 167 deletions.
38 changes: 19 additions & 19 deletions README.md
Expand Up @@ -233,7 +233,7 @@ client.get("foo_rand000000000000", function (err, reply) {
client.get(new Buffer("foo_rand000000000000"), function (err, reply) {
console.log(reply.toString()); // Will print `<Buffer 4f 4b>`
});
client.end();
client.quit();
```

retry_strategy example
Expand Down Expand Up @@ -302,7 +302,7 @@ client.get("foo_rand000000000000", function (err, reply) {
});
```

`client.end()` without the flush parameter should NOT be used in production!
`client.end()` without the flush parameter set to true should NOT be used in production!

## client.unref()

Expand Down Expand Up @@ -377,34 +377,34 @@ client connections, subscribes to a channel on one of them, and publishes to tha
channel on the other:

```js
var redis = require("redis"),
client1 = redis.createClient(), client2 = redis.createClient(),
msg_count = 0;
var redis = require("redis");
var sub = redis.createClient(), pub = redis.createClient();
var msg_count = 0;

client1.on("subscribe", function (channel, count) {
client2.publish("a nice channel", "I am sending a message.");
client2.publish("a nice channel", "I am sending a second message.");
client2.publish("a nice channel", "I am sending my last message.");
sub.on("subscribe", function (channel, count) {
pub.publish("a nice channel", "I am sending a message.");
pub.publish("a nice channel", "I am sending a second message.");
pub.publish("a nice channel", "I am sending my last message.");
});

client1.on("message", function (channel, message) {
console.log("client1 channel " + channel + ": " + message);
sub.on("message", function (channel, message) {
console.log("sub channel " + channel + ": " + message);
msg_count += 1;
if (msg_count === 3) {
client1.unsubscribe();
client1.end();
client2.end();
sub.unsubscribe();
sub.quit();
pub.quit();
}
});

client1.subscribe("a nice channel");
sub.subscribe("a nice channel");
```

When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into a "subscriber" mode.
At that point, only commands that modify the subscription set are valid. When the subscription
At that point, only commands that modify the subscription set are valid and quit (and depending on the redis version ping as well). When the subscription
set is empty, the connection is put back into regular mode.

If you need to send regular commands to Redis while in subscriber mode, just open another connection.
If you need to send regular commands to Redis while in subscriber mode, just open another connection with a new client (hint: use `client.duplicate()`).

## Subscriber Events

Expand All @@ -413,13 +413,13 @@ If a client has subscriptions active, it may emit these events:
### "message" (channel, message)

Client will emit `message` for every message received that matches an active subscription.
Listeners are passed the channel name as `channel` and the message Buffer as `message`.
Listeners are passed the channel name as `channel` and the message as `message`.

### "pmessage" (pattern, channel, message)

Client will emit `pmessage` for every message received that matches an active subscription pattern.
Listeners are passed the original pattern used with `PSUBSCRIBE` as `pattern`, the sending channel
name as `channel`, and the message Buffer as `message`.
name as `channel`, and the message as `message`.

### "subscribe" (channel, count)

Expand Down
8 changes: 7 additions & 1 deletion changelog.md
Expand Up @@ -5,15 +5,21 @@ Changelog

Features

- Monitor now works together with the offline queue
- Monitor and pub sub mode now work together with the offline queue
- All commands that were send after a connection loss are now going to be send after reconnecting
- Activating monitor mode does now work together with arbitrary commands including pub sub mode
- Pub sub mode is completly rewritten and all known issues fixed

Bugfixes

- Fixed calling monitor command while other commands are still running
- Fixed monitor and pub sub mode not working together
- Fixed monitor mode not working in combination with the offline queue
- Fixed pub sub mode not working in combination with the offline queue
- Fixed pub sub mode resubscribing not working with non utf8 buffer channels
- Fixed pub sub mode crashing if calling unsubscribe / subscribe in various combinations
- Fixed pub sub mode emitting unsubscribe even if no channels were unsubscribed
- Fixed pub sub mode emitting a message without a message published

## v.2.5.3 - 21 Mar, 2016

Expand Down

0 comments on commit 7a5a4aa

Please sign in to comment.