Real-time notifications
Broadcast an event to all currently connected consumers of a topic.
Redis · Command
PUBLISH sends a message to a channel for delivery to its current subscribers. It returns the number of clients that received the message.
PUBLISH sends a message to every client currently subscribed to the specified channel. In a Redis Cluster, Redis forwards the publication as needed, but the returned count includes only clients connected to the publisher's node.
PUBLISH channel message| Name | Type | Description |
|---|---|---|
channel | channel | Channel to publish the message to. |
message | message | Message to publish. |
Integer reply: the number of clients that the message was sent to.
redis-cli SUBSCRIBE news
redis-cli PUBLISH news "Redis update"Run the subscriber in one terminal and the publisher in another; the publisher receives the number of clients that received the message.
Broadcast an event to all currently connected consumers of a topic.
Signal a waiting service that an operation completed without polling.
Fan out a serialized application event to active listeners.
PUBLISH takes a channel and a message, then returns an integer representing the number of clients that received it. The command does not require subscribers to exist.
Follow-up: How does this count behave in a cluster? It includes only clients connected to the same node as the publishing client.
The command uses the syntax PUBLISH channel message. The channel identifies the publication topic, while the message is the payload delivered to subscribers.
Follow-up: How can clients receive pattern-based publications? They can use PSUBSCRIBE; Redis sends those deliveries as pattern-matching messages.
PUBLISH has time complexity O(N+M), where N is the number of clients subscribed to the receiving channel and M is the total number of subscribed patterns.
Follow-up: What delivery guarantee does Redis Pub/Sub provide? At-most-once delivery: a message is delivered once if at all.
PUBLISHSUBSCRIBEUNSUBSCRIBEPSUBSCRIBESPUBLISH