发布订阅
命令
- 订阅
subscribe 频道名 [频道名 ...]
例如:创建一个频道名为animal_channel
127.0.0.1:6379> subscribe animal_channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "animal_channel"
3) (integer) 1
- 发布消息
publish 频道 消息
例如:重新开一个终端,给animal_channel发送消息
127.0.0.1:6379> publish animal_channel 'panda is cute '
(integer) 1
127.0.0.1:6379> publish animal_channel 'I like panda'
(integer) 1
开启了订阅的anim_channel频道的终端接收到的数据
127.0.0.1:6379> subscribe animal_channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "animal_channel"
3) (integer) 1
1) "message"
2) "animal_channel"
3) "panda is cute "
1) "message"
2) "animal_channel"
3) "I like panda"
- 取消订阅
- 如果不写频道名,则表示取消所有频道
unsubscribe 频道名 [频道名 ...]
例如:取消已经订阅的animal_channel
127.0.0.1:6379> unsubscribe animal_channel
1) "unsubscribe"
2) "animal_channel"
3) (integer) 0