w3resource

Redis Pub/Sub: SUBSCRIBE

Redis SUBSCRIBE Command

Redis SUBSCRIBE command is used to subscribe the client to the specified channels.

Here are some common uses and scenarios for the "SUBSCRIBE" command:

  • Channel Subscription: Subscribes the client to one or more specified channels, enabling it to receive messages published on those channels.
  • Real-Time Communication: Facilitates real-time communication by allowing clients to listen for and process messages as they are published.
  • Event Handling: Useful for implementing event-driven architectures where clients react to events published on specific channels.
  • Message Broadcasting: Enables message broadcasting to multiple clients subscribed to the same channel, supporting use cases like chat applications or notifications.

Syntax:

SUBSCRIBE channel [channel ...]

Available since

2.0.0.

Return Value

Array reply.

Return Value Type

Array

Example: Redis SUBSCRIBE

redis 127.0.0.1:6379> SUBSCRIBE mychannel 
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1
1) "message"
2) "mychannel"
3) "a"

Previous: PUNSUBSCRIBE
Next: UNSUBSCRIBE



Follow us on Facebook and Twitter for latest update.