Redis 发布订阅

Pub/Sub 做一对多的实时消息广播。

Beginner
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.

Friends & Partners