在Redis中发布订阅功能是由publish、subscribe、unsubscribe、psubscribe组成的。
publish是发布,subscribe是订阅,unsubscribe是取消订阅,psubscribe是订阅模式,所谓模式也就是可以对应多个频道的“频道”,例如订阅了[abc]hello模式,就会收到ahello频道、bhello频道、chello频道的消息。
订阅者可以订阅一个或多个频道,发送者可以给指定的频道发布消息
subscribe channel [channel ...],订阅一个或多个频道,例如订阅cctv1这个频道
publish channel message,另一个客户端可以发布信息,发布成功之后,我们可以看到订阅者收到了信息
ctrl + c退出
如果订阅一个模式
127.0.0.1:6379> PSUBSCRIBE [abc]hello
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "[abc]hello"
3) (integer) 1
不管是发布ahello、bhello、chello,都会收到消息
127.0.0.1:6379> PUBLISH ahello "hello world"
(integer) 1
127.0.0.1:6379> PUBLISH bhello "hello world"
(integer) 1
127.0.0.1:6379> PUBLISH chello "hello world"
(integer) 1
下面是接收
127.0.0.1:6379> pSUBSCRIBE [abc]hello
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "[abc]hello"
3) (integer) 1
1) "pmessage"
2) "[abc]hello"
3) "ahello"
4) "hello world"
1) "pmessage"
2) "[abc]hello"
3) "bhello"
4) "hello world"
1) "pmessage"
2) "[abc]hello"
3) "chello"
4) "hello world"