ruby如何使用redis订阅/发布

redis提供的发布订阅命令
subscribe: subscribe channel [channel …] 订阅一个或多个频道
unsubscribe: unsubscribe [channel [channel …]] 退订频道,如果没有指定频道,则退订所有的频道
publish: publish channel message 给指定的频道发消息
psubscribe: psubscribe pattern [pattern …] 订阅给定模式相匹配的所有频道
punsubscribe: punsubscribe [pattern [pattern …]] 退订给定的模式,如果没有指定模式,则退订所有模式

ruby中订阅一个频道test_channel(长连接)

channel_name = "test_channel"

redis = Redis.current
redis.subscribe(channel_name) do |on|
    on.message do |channel, message|
        Rails.logger.info("listen #{channel}: #{message}")
    end
end

然后在另一个console中redis发布

redis = Redis.current
redis.publish('test_channel', 'hello')
原文地址:https://www.cnblogs.com/wangyuyu/p/13261730.html