A publish Subscription (PUB/SUB) is a message communication pattern that primarily relieves the coupling of communication between a message publisher and a message subscriber.
As a pub/sub server, Redis serves as a message routing feature between subscribers and publishers. Subscribers can subscribe to the Redis server for the type of message they are interested in by using the Subscribe and Psubscribe commands, and Redis is calling the information type Channel. When a Publisher uses the Publish command to send a specific type of information to Redis server, all clients subscribing to that type of information receive this message.
A client via SUBSCRIBE subscription channel TV1
127.0.0.1:6379> SUBSCRIBE TV1
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "TV1"
3) (integer) 1
1) "Message"
2) "TV1"
3) "Hexu"
b client via SUBSCRIBE subscription channel TV1 TV2
127.0.0.1:6379> SUBSCRIBE TV1 TV2
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "TV1"
3) (integer) 1
1) "Subscribe"
2) "TV2"
3) (integer) 2
Publisher TV1 message via PUBLISH command is Hexu, received by a client and B client
127.0.0.1:6379> PUBLISH TV1 Hexu
(integer) 2
Six. Redis Publishing subscription mechanism