Exact-topic event delivery
A service needs real-time notifications for a known topic such as orders:created.
Redis · Command
Subscribes the client to one or more exact channel names. Redis then pushes matching published messages and subscription confirmations through that connection.
SUBSCRIBE registers the current client for one or more exact channel names. After the client enters subscribed mode, Redis pushes matching published messages over the connection and sends a subscription confirmation for each channel; with RESP2, only a limited set of commands remains available until the client leaves that mode.
SUBSCRIBE channel [channel ...]| Name | Type | Description |
|---|---|---|
channel [channel ...] | string | One or more exact channel names to which the client should subscribe. |
On success, Redis does not return a conventional value. Instead, it pushes one subscription confirmation message for each requested channel, with subscribe as the first element.
SUBSCRIBE cache:eventsRun this command in redis-cli to enter Pub/Sub mode and listen for messages published to the exact channel cache:events. The client remains waiting for pushed messages until it unsubscribes or exits.
A service needs real-time notifications for a known topic such as orders:created.
Several known application topics should share one subscriber connection.
A lightweight client needs to observe live channel traffic during development or operations.
SUBSCRIBE takes one or more channel names and switches the connection into subscribed mode. Redis pushes a confirmation for each subscription, followed by messages published to those channels.
Follow-up: How would you subscribe to a family of channels? Use PSUBSCRIBE with a pattern, because SUBSCRIBE matches only the explicitly named channels.
The command's complexity is O(N), where N is the number of channels being subscribed to. Each specified channel contributes to the subscription operation.
Follow-up: What is the practical implication of subscribing to many channels? The command processes each channel, so the work grows linearly with the number supplied.
A RESP2 client in subscribed state is restricted to subscription-management commands and a few connection commands such as PING and QUIT. A RESP3 client can issue any commands while remaining subscribed.
Follow-up: How can a RESP2 client return to normal command use? It must unsubscribe from its channels and leave subscribed mode.
PSUBSCRIBEUNSUBSCRIBEPUNSUBSCRIBEPUBLISHSSUBSCRIBESUNSUBSCRIBEPINGRESET