Publish and subscribe to messages

Scenario introduction

Similar to Redis, ApsaraDB for Redis provides publishing (pub) and subscription (sub)
features. ApsaraDB for Redis allows multiple clients to subscribe to messages published
by a client.

It must be noted that messages published using ApsaraDB for Redis are "non-persistent".
This means the message publisher is only responsible for publishing a message and
does not save previously sent messages, whether or not these messages were received.
Thus, messages are "lost once published". Message subscribers can only receive messages
that are subscribed. They will not receive the earlier messages in the channel.

In addition, the message publisher (publish client) does not necessarily connect to
a server exclusively. While publishing messages, you can perform other operations
(for example, the List operations) from the same client at the same time. However,
the message subscriber (subscribe client) needs to connect to a server exclusively.
That is, during the subscription period, the client may not perform any other operations.
Rather, the operations are blocked while the client is waiting for messages in the
channel. Therefore, message subscribers must use a dedicated server connection or
thread (see the following example).

The preceding example demonstrates a situation with one publisher and one subscriber.
There can be multiple publishers, subscribers, and even multiple message channels.
In such scenarios, you are required to slightly change the code to fit the scenario.