Redis supports PUBSUB. Subscribing is easy enough:
redis 127.0.0.1:6379> subscribe foo
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "foo"
3) (integer) 1
However it seems impossible to unsubscribe, because while subscribing, the server does not accept commands. e.g. in the redis-cli
client that ships with redis, control is not returned to the client, so if I type unsubscribe
it doesn't go anywhere.
This seems like either a blatant error in the documentation, the function, or a PEBKAC issue. What gives?
Version:
$ ./redis-server --version
Redis server v=2.6.14 sha=00000000:0 malloc=libc bits=64
Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels.
Redis' pub/sub sends messages to clients subscribed (listening) on a channel. If you are not listening, you will miss the message (hence the blocking call). If you want to have it non-blocking, I recommend using a queue instead (redis is pretty good at that too).
Regarding the number of subscribers and publishers, it is limited by the maxclients setting, 10,000 by default. There is no limitation for subscribers and publishers, but the maximum clients (connections) limit applies.
By client, I believe they mean the list of clients here:
http://redis.io/clients
As someone who has consumed the hiredis client, I presume that this recommendation:
Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands.
on this page: http://redis.io/commands/subscribe applies only to those clients.
The redis-cli is among those clients. So, the comment is not an instruction for users of redis-cli.
Instead, redis-cli blocks waiting for messages on the bus (only to be unsubcribed via a ctrl+c).
If you were to use a different client (or more specifically, if you were implementing one), my guess is that you'd have to observe that convention so that it would be in a subscribed state (though the client wouldn't necessarily be blocking).
I think the documentation could be a little clearer to disambiguate that; however, the documentation is on the server itself and not the redis-cli application. You could, however, make the adjustment in the documentation repo and submit a pull request.
https://github.com/antirez/redis-doc/blob/master/commands/subscribe.md
Actually, PSUBSCRIBE will also block all subsequent commands as same as SUBSCRIBE, thus you can't ship any orders to the server but cast your eager gaze back to wait your interested channel for the incoming messages. Well, This ridiculous behavior makes my head spin.
However, if you try to interact with redis by means of telnet (e.g telnet localhost 6379
) instead of redis-cli prompt. Everything will be OK. Enjoy it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With