How to use UNSUBSCRIBE command in Redis 2.6.11

2019-04-13 12:55发布

Published a message to a particular channel.

redis 127.0.0.1:6379> PUBLISH channel message
(integer) 0

Using another Redis client I subscribed the channel .

redis 127.0.0.1:6379> SUBSCRIBE channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel"
3) (integer) 1

In the Redis client I got all the Published messages. Now I want to unsubscribe from the subscribed channel. But I cant type unsubscribe in the Redis client. When I use Ctrl+c full Redis client quits. How to write the Unsubscribe command in Redis Client?

标签: redis
1条回答
趁早两清
2楼-- · 2019-04-13 13:35

I dont think you can issue unsubscribe in the client cause the client was blocked. I wrote a ruby script to show how to use unsubscribe.

require 'redis'
r = Redis.new
r.subscribe 'first' do |on|
  on.message do |e, d|
    puts e
    puts d
    r.unsubscribe
  end
end
puts "script was blocked?"

If you remove r.unsubscribe, the script will be blocked. And you can add if clause to check when to unsubscribe client.ex:

r.unsubscribe if d == 'leave'
查看更多
登录 后发表回答