A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message.
Redis clients can subscribe to any number of channels.
Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:
When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:
Instance
The following example shows how a publish subscription works. In our example we created a subscription channel named Redischat:
Redis127.00.1:6379> SUBSCRIBE redischatreading Messages ... (press ctrl- c to Quit) 1) "subscribe" 2 "redischat" 3 (integer 1
Now, we re-open a Redis client and then publish two messages on the same channel Redischat, and the Subscriber can receive the message.
Redis127.0.0.1:6379>PUBLISH Redischat"Redis is a great caching technique"(Integer) 1Redis127.0.0.1:6379>PUBLISH Redischat"Learn Redis by w3cschool.cc"(Integer) 1# subscriber's client will display the following message < Span class= "lit" >1) "message" 2) "redischat" 3 "Redis is a great caching technique" 1) "message" < Span class= "lit" >2) "redischat" 3) "learn Redis by w3cschool.cc"
Psubscribe pattern [pattern ...] subscribes to one or more channels that match a given pattern
PUBSUB subcommand [argument [argument ...]] is used to view subscriptions and publish System State, which consists of several subcommands in different formats.
Publish channel message is used to send information to the specified channel, returning the number of subscribers receiving the information
Subscribe Channel [Channel ...] information for subscribing to a given channel or channels
Redis Publish Subscriptions