SUBSCRIBE

Since 1.1.0Pub/Sub

Syntax

SUBSCRIBE channel [channel ...]
  • Flags: pubsub
  • ACL categories: @pubsub, @slow
  • Arity: -2

Subscribes this connection to channels. Each message PUBLISHed to one of them then arrives as a three-element array: message, the channel, the message.

A subscribed connection speaking RESP2 can only send SUBSCRIBE, PSUBSCRIBE, SSUBSCRIBE, their UNSUBSCRIBE commands, PING, QUIT and RESET until it unsubscribes from everything. One speaking RESP3 (after HELLO 3) can send any command, and messages arrive as push replies between the replies to its commands. Subscribing twice to a channel delivers its messages once.

Reply

  • For each channel, an acknowledgement: subscribe, the channel, and how many subscriptions the connection now has.

Examples

> SUBSCRIBE news sport
1) "subscribe"
2) "news"
3) (integer) 1
1) "subscribe"
2) "sport"
3) (integer) 2
> GET k
(error) ERR Can't execute 'get': only (P|S)SUBSCRIBE / (P|S)UNSUBSCRIBE / PING / QUIT / RESET are allowed in this context