Category: Redis
Aysnchronous messaging between a publisher and subscribers improves application scalability, resilience, and supports use of a dynamic network topology. Redis supports this messaging pattern using pub/sub channels.
Assume you want to subscribe to two channels called "purchases" and "registrations". Issue the following command using the Redis CLI:
127.0.0.1:6379> SUBSCRIBE purchases registrations
Another client can publish a message to either of these channels. In this example, we will notify subscribers to the "registrations" channel that a user with the email address buddy.guy@jazzclub.com
registered for using a hypothetical system.
127.0.0.1:6379> PUBLISH registrations buddy.guy@jazzclub.com
To unsubscribe from a channel, issue the following command:
127.0.0.1:6379> UNSUBSCRIBE purchases registrations
If no channel names are provided, the client will unsubscribe from all channels.
Note: Pub/Sub has no relation to the database key space. Scoping can be achieved using a prefix for the channel name.