Redis Publish & Subscribe Transactions Tutorial
In this Redis tutorial we learn how to send and receive messages with the Publish & Subscribe transaction model.
We cover how to publish to channels, subscribe and unsubscribe as well as returning all the available channels.
What is the Publish & Subscribe transaction model
Redis allows us to send messages to different clients by using the Publish & Subscribe (Pub/Sub) transaction model. It allows the sender to send a message without it knowing the exact number of receivers.
Think of it as following someone on Twitter. Each time the person (Publisher) tweets a message, all the followers (Subscribers) will receive it and can view it on Twitter (Channel).
In Redis a client can subscribe to a channel, and a publisher can send messages through the channel to all of its subscribers.
How to subscribe to a channel
A client can subscribe to any channel with the SUBSCRIBE command.
This command accepts the channel names the client wants to subscribe to as arguments.
SUBSCRIBE channel_name-1 channel_name-2
When a client subscribes to a channel, it’s automatically created and made available to publishers to send messages.
SUBSCRIBE eurusd-tick
Redis will return the operation, the channel name(s) and the number of channels subscribed to.
1) "subscribe"
2) "eurusd-tick"
3) (integer) 1
We can also subscribe to channels matching a certain pattern by using the PSUBSCRIBE command.
PSUBSCRIBE pattern
PSUBSCRIBE h*
The example above will subscribe to all channels that starts with the letter ‘h’.
How to publish messages to a channel
A publisher can send messages through any channel, with at least one subscriber, by using the PUBLISH command. This command accepts the message to send as an argument.
Continuing with the example from above, open another command line (or another tab if yours supports it) and get into the Redis CLI.
If you’re running Docker, type in the following.
docker exec -it image_name sh
Where ‘image_name’ is the name of your Redis image. In our case it was ‘test-redis’.
docker exec -it test-redis sh
This will give you access to the image’s interactive shell. From here it’s easy to get into the Redis CLI.
redis-cli
We’ll use this client to publish messages to the one that’s already subscribed to ‘eurusd-tick’.
PUBLISH channel_name
PUBLISH eurusd-tick 1.17057
If we switch over to the subscriber client, we can see that the message was received.
1) "message"
2) "eurusd-tick"
3) "1.17057"
We can also publish messages to channels matching a pattern.
PUBLISH h* "This one goes out to all the single ladies"
The example above will send the message to all channels that starts with the letter ‘h’.
How to unsubscribe from a channel
We can stop listening for messages from certain channels by using the UNSUBSCRIBE command. This command takes the channel(s) we want to unsubscribe from as arguments.
UNSUBSCRIBE channel_name-1 channel_name-2
The subscriber will have to stop listening for messages by pressing Ctrl+C before it can unsubscribe from a channel.
This may throw you out of the Redis CLI, simply type redis-cli to get back in again.
UNSUBSCRIBE eurusd-tick
1) "unsubscribe"
2) "eurusd-tick"
3) (integer) 0
We can also unsubscribe to channels matching a pattern by using the PUNSUBSCRIBE command.
PUNSUBSCRIBE pattern
PUNSUBSCRIBE h*
The example above will unsubscribe from all channels that starts with the letter ‘h’.
How to return all the available channels
We can return all the active channels by using the PUBSUB subcommand with the CHANNELS argument.
PUBSUB CHANNELS
The command will return an array of active channels. If no channels are active (subscribed to), it will return an empty array.
(empty array)