w3resource

Redis Pub/Sub: PSUBSCRIBE

Redis PSUBSCRIBE Command

Redis PSUBSCRIBE command is used to subscribe to channels matching the given patterns.

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

  • Pattern-Based Subscriptions: Subscribes to multiple channels using pattern matching, allowing clients to receive messages from channels that match a specified pattern.
  • Event-Driven Architecture: Facilitates building event-driven architectures by enabling clients to listen for events on channels that follow naming conventions.
  • Scalable Messaging: Supports scalable messaging systems by allowing clients to subscribe to a range of channels with a single pattern.
  • Dynamic Channel Monitoring: Useful for monitoring and reacting to dynamic sets of channels without needing to know exact channel names in advance.

Syntax:

PSUBSCRIBE CHANNEL_NAME_OR_PATTERN [PATTERN...]

Following listing shows some supported patterens in redis

  • h?llo subscribes to hello, hallo and hxllo
  • h*llo subscribes to hllo and heeeello
  • h[ae]llo subscribes to hello and hallo, but not hillo

Available since

2.0.0.

Example: Redis PSUBSCRIBE

redis 127.0.0.1:6379> PSUBSCRIBE mychannel
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "mychannel"
3) (integer) 1

Previous: PFMERGE
Next: PUBSUB



Follow us on Facebook and Twitter for latest update.