Redis publish and subscribe (pub/sub) is a message communication mode: the sender (pub) sends messages, and the subscriber (sub) receives messages. Its publish and subscribe functions are composed of commands such as PUBLISH, SUBSCRIBE, and PSUBSCRIBE.
By executing the SUBSCRIBE command, a client can subscribe to one or more channels and become subscribers of these channels: whenever another client sends a message to the subscribed channel, all subscribers of the channel will receive this message.
Be lazy today to simulate the process of publishing and subscribing.
First start the server on port 6379:
Then start three clients through the redis-cli command, two of which execute
subscribe "newit" respectively //Subscribe to the newit channel
Now we publish a message to newit on the third client:
Now observe whether the two subscribed clients receive the message:
ok received the message.