Redis基础系列-0x008:发布订阅模式

0x001 订阅一个或者多个频道

命令格式:
SUBSCRIBE channel [channel channel ...]

$ redis-cli
# 进程1
127.0.0.1:6379> SUBSCRIBE chat chat2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chat"
3) (integer) 1
1) "subscribe"
2) "chat2"
3) (integer) 2

0x002 发布消息

命令格式:
PUBLISH channel message

$ redis-cli
# 进程2
127.0.0.1:6379> PUBLISH chat hellochat1
(integer) 1
127.0.0.1:6379> PUBLISH chat2 hellochat2
(integer) 1
#此时的进程1
127.0.0.1:6379> SUBSCRIBE chat chat2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chat"
3) (integer) 1
1) "subscribe"
2) "chat2"
3) (integer) 2
1) "message"
2) "chat"
3) "hellochat1"
1) "message"
2) "chat2"
3) "hellochat2"
    原文作者:followWinter
    原文地址: https://segmentfault.com/a/1190000014911648
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞