You are required to use two connections for pub and sub. A subscriber connection cannot issue any commands other than subscribe
, psubscribe
, unsubscribe
, punsubscribe
(although @Antirez has hinted of a subscriber-safe ping
in the future). If you try to do anything else, redis tells you:
-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context
(note that you can't test this with redis-cli, since that understands the protocol well enough to prevent you from issuing commands once you have subscribed - but any other basic socket tool should work fine)
This is because subscriber connections work very differently - rather than working on a request/response basis, incoming messages can now come in at any time, unsolicited.
publish
is a regular request/response command, so must be sent on a regular connection, not a subscriber connection.