Redis消息订阅

使用办法:

命令 作用
publish channel content 向指定channel发布内容
subscribe channel [channel …] 订阅指定channel消息
psubscribe pattern [pattern …] 订阅符合pattern的channel,支持?、*、[]进行通配,如new[sn],可订阅news和newn的消息
unsubscribe [channel [channel …]] 取消订阅channel消息
punsubscribe [pattern [pattern …]] 取消订阅pattern的channel,支持?、*、[]进行通配,如new[sn],可取消订阅news和newn的消息
pubsub numsub channel … 查看某个或多个channel订阅者数量
pubsub numpat 查看所有订阅者数量

测试案例1:
消息发布端:

127.0.0.1:6379> publish news 'hello redis'
(integer) 1
127.0.0.1:6379> publish news 'hello publish'
(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) "hello redis"
1) "message"
2) "news"
3) "hello publish"

测试案例2
消息发布端:

127.0.0.1:6379> publish news 'hello redis'
(integer) 1
127.0.0.1:6379> publish newn 'hello word'
(integer) 1
127.0.0.1:6379>

消息订阅端:

127.0.0.1:6379> psubscribe new[sn]
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "new[sn]"
3) (integer) 1
1) "pmessage"
2) "new[sn]"
3) "news"
4) "hello redis"
1) "pmessage"
2) "new[sn]"
3) "newn"
4) "hello word"

你可能感兴趣的:(redis,订阅,消息)