Redis Pub/Sub Messaging

RedisBeginner
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.

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.

Terminal 1: Subscribe to Channel

Open your first terminal (we'll call this Terminal 1). You can use the Xfce terminal provided in the LabEx environment.

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

redis-cli

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

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.

Important: Keep Terminal 1 open with the redis-cli session active and subscribed to the channel. Do NOT close this terminal or press Ctrl+C, as this would unsubscribe you from the channel. We'll use a second terminal for publishing messages in the next step.

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.

Terminal 2: Publish Messages

Open a second terminal (we'll call this Terminal 2). You can open a new tab in your Xfce terminal.

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 in Terminal 2 will be the number of clients that received the message:

(integer) 1

In this case, the output is (integer) 1 because you have an active subscription to mychannel in Terminal 1 from the previous step.

Observe the Message in Terminal 1

Now switch back to Terminal 1 (where you subscribed to mychannel in Step 1). You should see the message you just published displayed in real-time:

1) "message"
2) "mychannel"
3) "Hello, Redis!"

This shows:

  • "message": Indicates this is a published message
  • "mychannel": The channel the message was published to
  • "Hello, Redis!": The actual message content

Test Real-time Messaging

You can now experiment with real-time messaging:

  1. Go back to Terminal 2 and publish more messages
  2. Switch to Terminal 1 to see them appear instantly
  3. Try publishing different messages to observe the live communication

Example - in Terminal 2:

PUBLISH mychannel "This is message 2"
PUBLISH mychannel "Real-time messaging works!"

Important: Keep both Terminal 1 and Terminal 2 with their redis-cli sessions active. We'll continue using both terminals in the next steps.

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.

Terminal 1: Exit Subscription Mode

Go to Terminal 1 (where you are currently subscribed to mychannel). You should see the subscription status showing "Reading messages... (press Ctrl-C to quit)".

Since Terminal 1 is in subscription mode, you cannot execute regular Redis commands directly. To unsubscribe, you need to exit the subscription mode first:

  1. Press Ctrl+C to exit the subscription mode

  2. You should see that the redis-cli session ends and returns to the terminal prompt

  3. Reconnect to Redis:

    redis-cli
  4. Now you can execute the UNSUBSCRIBE command (though it's not necessary since disconnecting already unsubscribed you):

    UNSUBSCRIBE mychannel

Note: When you pressed Ctrl+C, you actually already disconnected from all subscriptions. The UNSUBSCRIBE command is shown for demonstration purposes, but in practice, disconnecting automatically unsubscribes you from all channels.

The output in Terminal 1 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.

Terminal 2: Test Publishing After Unsubscribe

Now switch to Terminal 2 and publish another message to mychannel:

PUBLISH mychannel "Is anyone still there?"

The output in Terminal 2 will be:

(integer) 0

This indicates that no clients received the message because you unsubscribed from the channel in Terminal 1.

Verify No Message Received

Look at Terminal 1 - you should notice that no new message appears because you have unsubscribed from the channel.

This demonstrates how unsubscribing from a channel prevents you from receiving further messages published to that channel, while the publisher can still send messages (but no one will receive them).

Important: Keep both terminals and their redis-cli sessions active for the next step.

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.

Terminal 1: Pattern Subscribe

Since you unsubscribed from mychannel in the previous step, Terminal 1 should now show a regular Redis prompt.

In Terminal 1, 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.

Terminal 2: Publish to Pattern-Matching Channels

Go to Terminal 2 and publish a message to the news.sports channel using the PUBLISH command:

PUBLISH news.sports "Sports news update!"

The output in Terminal 2 will be the number of subscribers that received the message:

(integer) 1

Observe Pattern Matching

Switch back to Terminal 1 (where you subscribed to the pattern). You should see the message you just published:

1) "pmessage"
2) "news.*"
3) "news.sports"
4) "Sports news update!"

This shows:

  • "pmessage": Indicates this is a pattern-matched message
  • "news.*": The pattern that matched
  • "news.sports": The actual channel the message was published to
  • "Sports news update!": The message content

Test Multiple Channels

In Terminal 2, try publishing to different channels that match the pattern:

PUBLISH news.technology "New AI breakthrough!"
PUBLISH news.weather "Sunny skies ahead!"
PUBLISH sports.basketball "This won't match the pattern"

Observe in Terminal 1 that only the news.* channels are received, while sports.basketball is not because it doesn't match the news.* pattern.

Important: Keep both terminals and their redis-cli sessions active. The pattern subscription demonstrates how you can listen to multiple related channels simultaneously.

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.