Redis 发布订阅
Pub/Sub 做一对多的实时消息广播。
Key Points发布者发到频道,所有订阅者实时收到;消息不持久,订阅前发的收不到。
发布与订阅
Shell
# 终端 A:订阅
SUBSCRIBE news
# 终端 B:发布
PUBLISH news "hello"Output
# 终端 A 收到:
1) "message"
2) "news"
3) "hello"
(integer) 1模式订阅
Shell
PSUBSCRIBE news.* # 订阅所有 news. 开头的频道
PUBLISH news.sport "goall!" # 匹配上Pub/Sub messages are not persisted: messages sent while a subscriber is offline are lost, with no ack. For reliable delivery use Stream. Pub/Sub fits live broadcast like real-time notifications.
发布与订阅
Shell
# 终端 A:订阅
SUBSCRIBE news
# 终端 B:发布
PUBLISH news "hello"Output
# 终端 A 收到:
1) "message"
2) "news"
3) "hello"
(integer) 1模式订阅
Shell
PSUBSCRIBE news.* # 订阅所有 news. 开头的频道
PUBLISH news.sport "goall!" # 匹配上Pub/Sub 消息不持久:订阅者下线期间的消息会丢,且没有确认机制。要可靠投递用 Stream。Pub/Sub 适合「在线广播」类场景,如多人协作的实时通知。