Redis Pub/Sub Messaging

RedisRedisBeginner
Practice Now

Introduction

In this lab, you will explore Redis's Pub/Sub messaging system. You'll learn how to subscribe to channels, publish messages, and unsubscribe. This lab will guide you through the fundamental operations using the SUBSCRIBE, PUBLISH, UNSUBSCRIBE, and PSUBSCRIBE commands.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL redis(("Redis")) -.-> redis/RedisGroup(["Redis"]) redis/RedisGroup -.-> redis/access_cli("Connect Using CLI") subgraph Lab Skills redis/access_cli -.-> lab-552102{{"Redis Pub/Sub Messaging"}} end

Subscribe to a Channel with SUBSCRIBE

In this step, you will learn how to subscribe to a channel in Redis using the SUBSCRIBE command. Subscribing to a channel allows you to receive messages published to that channel. This is a fundamental concept in Redis's Pub/Sub messaging system.

First, open a terminal. You can use the Xfce terminal provided in the LabEx environment.

Now, connect to the Redis server using the redis-cli command:

redis-cli

You should see the Redis prompt: 127.0.0.1:6379>.

Next, subscribe to a channel named mychannel using the SUBSCRIBE command:

SUBSCRIBE mychannel

The output will look similar to this:

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1

Let's break down the output:

  • "subscribe": This indicates that you have successfully subscribed to a channel.
  • "mychannel": This is the name of the channel you subscribed to.
  • (integer) 1: This is the number of channels you are currently subscribed to.

It's important to keep this terminal open and subscribed to the channel. We'll use another terminal to publish messages.

Now, exit the redis-cli in this terminal. This is important for the command history to be logged correctly. Press Ctrl+C to stop the subscription, then type exit and press Enter.

exit

Publish Messages with PUBLISH

In this step, you will learn how to publish messages to a channel in Redis using the PUBLISH command. Publishing messages is the core functionality of the Pub/Sub system, allowing you to send data to all subscribers of a specific channel.

Open a new terminal. You can use the Xfce terminal provided.

Connect to the Redis server using the redis-cli command:

redis-cli

You should see the Redis prompt: 127.0.0.1:6379>.

Now, publish a message to the mychannel channel using the PUBLISH command:

PUBLISH mychannel "Hello, Redis!"

The output will be the number of clients that received the message:

(integer) 1

In this case, the output is (integer) 1 because you subscribed to mychannel in the previous step. If you hadn't subscribed, the output would be (integer) 0.

Now, exit the redis-cli in this terminal.

exit

Switch back to the first terminal (where you subscribed to mychannel in Step 1). You should see the message you just published. If you closed that terminal, you'll need to open a new terminal and re-subscribe to mychannel using the SUBSCRIBE mychannel command.

redis-cli
SUBSCRIBE mychannel

Keep this terminal open and subscribed.

Unsubscribe with UNSUBSCRIBE

In this step, you will learn how to unsubscribe from a channel in Redis using the UNSUBSCRIBE command. Unsubscribing removes a client from the list of subscribers for a specific channel, preventing it from receiving further messages published to that channel.

Continuing from the previous steps, you should have one terminal open and subscribed to mychannel.

In that terminal, execute the following command to unsubscribe from the mychannel channel:

UNSUBSCRIBE mychannel

The output will look like this:

1) "unsubscribe"
2) "mychannel"
3) (integer) 0

Let's break down the output:

  • "unsubscribe": This indicates that you have unsubscribed from a channel.
  • "mychannel": This is the name of the channel you unsubscribed from.
  • (integer) 0: This is the number of channels you are currently subscribed to. It's now 0 because you unsubscribed from mychannel.

Now, exit the redis-cli in this terminal.

exit

Open a new terminal and connect to Redis:

redis-cli

Publish another message to mychannel:

PUBLISH mychannel "Is anyone still there?"

The output in this terminal will be:

(integer) 0

This indicates that no clients received the message because you unsubscribed in the previous step.

Now, exit the redis-cli in this terminal.

exit

This demonstrates how to unsubscribe from a channel, preventing you from receiving further messages published to that channel.

Pattern Subscribe with PSUBSCRIBE

In this step, you will learn how to subscribe to channels using patterns with the PSUBSCRIBE command in Redis. PSUBSCRIBE allows you to subscribe to multiple channels that match a specified pattern. This is useful when you want to receive messages from a group of related channels without subscribing to each one individually.

Open a terminal.

Connect to the Redis server using the redis-cli command:

redis-cli

Now, subscribe to channels matching the pattern news.* using the PSUBSCRIBE command:

PSUBSCRIBE news.*

The output will look similar to this:

Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "news.*"
3) (integer) 1

Let's break down the output:

  • "psubscribe": This indicates that you have successfully subscribed to a pattern.
  • "news.*": This is the pattern you subscribed to. The .* is a wildcard that matches any characters.
  • (integer) 1: This is the number of patterns you are currently subscribed to.

Keep this terminal open and subscribed.

Now, exit the redis-cli in this terminal.

exit

Open a new terminal and connect to Redis:

redis-cli

Publish a message to the news.sports channel using the PUBLISH command:

PUBLISH news.sports "Sports news update!"

The output in this terminal will be the number of subscribers that received the message:

(integer) 1

Now, exit the redis-cli in this terminal.

exit

Switch back to the first terminal (where you subscribed to the pattern). You should see the message you just published. If you closed that terminal, you'll need to open a new terminal and re-subscribe to news.* using the PSUBSCRIBE news.* command.

redis-cli
PSUBSCRIBE news.*

Keep this terminal open and subscribed.

Summary

In this lab, you explored the fundamentals of Redis Pub/Sub messaging. You learned how to subscribe to channels using the SUBSCRIBE command, publish messages to channels using the PUBLISH command, unsubscribe from channels using the UNSUBSCRIBE command, and subscribe to channels using patterns with the PSUBSCRIBE command. These commands are essential for building real-time messaging applications with Redis.