在Redis中,你可以设定对某一个key值进行消息发布及消息订阅,当一个key值上进行了消息发布后,所有订阅它的客户端都会收到相应的消息。这一功能最明显的用法就是用作实时消息系统,比如普通的即时聊天,群聊等功能。
Pub/Sub 从字面上理解就是发布(Publish)与订阅(Subscribe)
命令原型 |
时间 |
命令描述 |
返回值 |
SUBSCRIBE channel [channel ...] |
O(1) |
订阅频道,可以订阅多个,比如auschat |
返回 |
PSUBSCRIBE pattern [pattern ...] |
O(1) |
按不同模式订制频道 |
成功返回1 失败返回0 |
UNSUBSCRIBE channel [channel ...] |
O(1) |
Redis UNSUBSCRIBE指令从给定的信道退订客户端,或者退订所有(如果没有给出) |
返回: |
PUNSUBSCRIBE [pattern [pattern ...]] |
O(1) |
PUNSUBSCRIBE命令从给定的模式退订客户端,或从所有如果没有给出。如果未指定任何模式,客户端是从所有先前认阅的模式退订 |
返回数组 |
PUBLISH channel message |
O(1) |
按频道发布消息 |
成功返回1 失败返回0 |
PUBSUB subcommand [argument [argument ...]] |
O(1) |
PubSub的命令是一个自省的命令,允许检查的Pub/Sub子系统的状态。它是由被分别记录的子命令。 |
返回数组 |
#A客户端链接redis数据库
[root@datahubdb1 bin]# ./redis-cli -a daphne
127.0.0.1:6379>
#subscribe、publish
#A客户端订阅key auschat
127.0.0.1:6379> subscribe auschat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "auschat"
3) (integer) 1
打开另外客户端连接
#B客户端链接redis数据库
[root@datahubdb1 bin]# ./redis-cli -a daphne
127.0.0.1:6379>
#B客户端按keyauschat发布消息
127.0.0.1:6379> publish auschat"asushiye is test publish"
#返回1 表示发布成功, 0 失败(可能A客户端没有订阅)
(integer) 1
#A客户端收到消息如下
127.0.0.1:6379> subscribe auschat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "auschat"
3) (integer) 1
#A客户端收到消息类型
1) "message"
#A客户端收到消息KEY
2) "auschat"
#A客户端收到消息内容
3) "asushiye is test publish"
#psubscribe、publish
#A客户端订阅key aus?chat 或key aus*chat 或 key aus[av]chat
127.0.0.1:6379> psubscribe aus?chat
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "aus?chat"
3) (integer) 1
127.0.0.1:6379> psubscribe aus*chat
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "aus*chat"
3) (integer) 1
127.0.0.1:6379> psubscribe aus[av]chat
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "aus[av]chat"
3) (integer) 1
#B客户端发布
#key aus?chat
127.0.0.1:6379> publish ausdchat"asushiye is test publish"
(integer) 1
127.0.0.1:6379> publish ausechat"asushiye is test publish"
(integer) 1
# key aus*chat
127.0.0.1:6379> publish ausechat"asushiye is test publish"
(integer) 1
127.0.0.1:6379> publish auseasdfchat"asushiye is test publish"
(integer) 1
# key aus[av]chat
127.0.0.1:6379> publish ausachat"asushiye a is test publish"
(integer) 1
127.0.0.1:6379> publish ausechat"asushiye a is test publish"
(integer) 0
127.0.0.1:6379> publish ausvchat"asushiye a is test publish"
(integer) 1
#unsubscribe、punsubscribe
#从给定的信道退订客户端
127.0.0.1:6379> unsubscribe aus[ab]chat
1) "unsubscribe"
2) "aus[ab]chat"
3) (integer) 0
#从给定的模式信道退订客户端
127.0.0.1:6379> punsubscribe aus?chat
1) "punsubscribe"
2) "aus?chat"
3) (integer) 0
#pubsub
#A客户端订阅key auschat
127.0.0.1:6379> subscribe auschat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "auschat"
3) (integer) 1
#C客户端
127.0.0.1:6379> pubsub channels
1) "auschat"
127.0.0.1:6379> pubsub numsub auschat
1) "auschat"
2) (integer) 1
#A客户端订阅key aus?chat
127.0.0.1:6379> psubscribe aus?chat
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "aus?chat"
3) (integer) 1
#C客户端
127.0.0.1:6379> pubsub numpat
(integer) 1
您觉的有所收获,请保持持续的关注。
您发现博客中有的纰漏,请指正。
您有更好的建议或更好的实现方式,请赐教。([email protected])