Redis-消息发布与订阅

Original 2016-11-09 14:05:38 481
abstract:redis的消息发布与订阅适合做在线聊天, 消息推送使用方法:发布端: publish +频道名称 + 发布内容订阅端: subscribe + 频道名称发布端例子:127.0.0.1:6379> publish news 'this is a test' (integer) 0 127.0.0.1:637

redis的消息发布与订阅适合做在线聊天, 消息推送

使用方法:

  • 发布端: publish +频道名称 + 发布内容

  • 订阅端: subscribe + 频道名称
    发布端例子:

127.0.0.1:6379> publish news 'this is a test'
(integer) 0
127.0.0.1:6379>

新开一个redis-cli
订阅端:

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

再次在发布端发布一个消息

127.0.0.1:6379> publish news 'another test'
(integer) 1    #返回的数字表示目前订阅这个频道的数量
127.0.0.1:6379>

查看订阅端

127.0.0.1:6379> subscribe news 
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "news"
3) (integer) 1
1) "message"
2) "news"
3) "another test"

psubscribe

模糊订阅, 比如订阅上例的news, 可以写成 __psubscribe ne*__ , 表示订阅所有 ne 开头的频道


Release Notes

Popular Entries