Upstash Documentation

PSUBSCRIBE

Subscribe to pattern channels.
2 min read

Use PSUBSCRIBE to subscribe the current connection to channels by glob-style pattern.

A pattern such as news.* matches every channel that starts with news., including channels created after the subscription, which is what makes patterns useful for topic hierarchies. ? matches a single character and [...] a character class.

Pattern subscriptions are tracked separately from the exact-channel subscriptions made with SUBSCRIBE, and a message that matches several of a connection's patterns is delivered once per matching pattern, so overlapping patterns produce duplicates. Cancel a pattern with PUNSUBSCRIBE, passing exactly the same pattern string.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<pattern>YesYesGlob-style channel pattern.

Important points#

  • This is a connection-oriented command and is available over native Redis TCP, not the stateless REST endpoint.
  • Subscription commands require a dedicated TCP connection. In RESP3, subscription events use push replies.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Three-element subscription-state array per pattern
RESP3Three-element subscription-state push reply per pattern
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs