Table of contents
1.
Introduction
2.
Redis Pub-Sub commands
3.
How to subscribe to a channel 
3.1.
Syntax
3.2.
Example
3.2.1.
Output
4.
How to publish messages to a channel
4.1.
Example
4.2.
Syntax
4.3.
Example
4.3.1.
Output
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pub/ Sub in Redis

Introduction

In this Redis tutorial, we'll go through how to create channels, subscribe and unsubscribe to them, and get a list of all accessible channels.

The Publish & Subscribe (Pub/Sub) transaction architecture in Redis allows us to transmit messages to many clients. It enables the sender to deliver a message without knowing how many people would receive it.

Consider it similar to following someone on Twitter. When a person (Publisher) tweets a message, it is sent to all of their followers (Subscribers), who may access it on Twitter (Channel).

A client can subscribe to a channel in Redis, and a publisher can deliver messages to all of the channel's subscribers.

Check our tool article to understand better.

Redis Pub-Sub commands

When using the publish-subscribe paradigm in Redis, there are two major commands to use:

  • SUBSCRIBE
     
  • PUBLISH
     

These instructions are simple to understand and define the task they do. The SUBSCRIBE command, for example, is used to subscribe a client to a certain channel or channel group.

A sender or publisher can use the PUBLISH command to broadcast a message to a defined number of channels.

  1. PSUBSCRIBE pattern
    Subscribes to channels that match the patterns specified.
     
  2. PUBSUB subcommand 
    Indicates the current condition of the Pub/Sub system. For instance, which clients are currently connected to the server.
     
  3. PUBLISH channel message
    Sends a message to a certain channel.
     
  4. PUNSUBSCRIBE 
    Stops listening for messages on channels that meet the specified patterns.
     
  5. SUBSCRIBE channel 
    Monitors messages sent to the specified channels.
     
  6. UNSUBSCRIBE 
    Turns off the listening for messages on the specified channels.

How to subscribe to a channel 

With the SUBSCRIBE command, a client can subscribe to any channel.

This command accepts the channel names that the client wishes to subscribe to.

Syntax

SUBSCRIBE channel_name-1 channel_name-2


A channel is instantly formed and made available to publishers when a client subscribes to it.

Example

SUBSCRIBE eurusd-tick


Redis will return the operation, the channel name(s), and the number of subscribed channels.

Output

  1. "subscribe"
  2. "eurusd-tick"
  3. (integer) 1

How to publish messages to a channel

Using the PUBLISH command, a publisher can deliver messages across any channel with at least one subscriber. As an input, this command accepts the message to send.

Continuing with the previous example, create a new command line (or a new tab if your system allows it) and enter the Redis CLI.

If you're using Docker, enter the following command.

Example

docker exec -it image_name sh


This client will be used to send messages to those who have previously subscribed to 'eurusd-tick.'

Syntax

PUBLISH channel_name

Example

PUBLISH eurusd-tick 1.17057


We can see that the message was received by switching to the subscriber client.

Output

  1. "message"
  2. "eurusd-tick"
  3. "1.17057"

Frequently Asked Questions

  1. What is the purpose of Redis Pubsub?
    Redis may be used as a Publisher/Subscriber platform in addition to data storage. Publishers can send messages to any number of subscribers on a channel using this pattern.
     
  2. What is the publish-subscribe pattern?
    Publish–subscribe is a communications pattern in which message senders, known as publishers, do not design messages to be delivered directly to specific receivers, known as subscribers, but instead, the group published messages into classes without knowing which subscribers, if any, may exist.
     
  3. What exactly is a Kafka pub/sub?
    Kafka is a Pub-sub and queue-based messaging system that is extremely fast, reliable, persistent, fault-tolerant, and has zero downtime. Furthermore, producers deliver the message to a subject, and the consumer may choose from various message systems.
     
  4. Is it possible to replace Kafka with the Redis stream?
    Redis streams are extremely similar to Kafka in terms of functionality. Unlike Pub/Sub, when messages are digested, they are not deleted from the stream. Redis streams may be consumed in two ways: blocking and nonblocking.
     
  5. What does it mean to publish in Redis?
    (PUBLISH channel message)
    This command sends a message to the specified channel. Clients can publish to any node in a Redis Cluster. Clients can subscribe to any channel by connecting to any node, and the cluster ensures that published messages are routed as needed.

Conclusion

In this Redis tutorial, we learned how to use the Publish & Subscribe transaction mechanism to send and receive messages. You can explore data types in Redis by clicking here.
Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Happy Coding!

Live masterclass