While testing some Groovy code that uses the Redis pub/sub functionality I wrote the script below. This is a simple example written in Groovy, it ships as a single file and loads the required dependencies at runtime via the Groovy Grape dependency management functionality.

Benefits of using Redis Pub Sub Functionality

Redis Pub Sub functionality can provide a number of benefits for applications that require real-time communication and data processing. Redis Pub Sub functionality allows for asynchronous messaging between different components of a system, enabling the creation of responsive and scalable applications.

Some benefits of using Redis Pub Sub include:

  • Scalability: Redis Pub Sub functionality enables the creation of distributed systems that can handle high volumes of data and messages. This can help improve system performance and scalability.
  • Real-time communication: Redis Pub Sub functionality allows for real-time messaging between different components of a system, enabling real-time updates and communication between different parts of an application.
  • Decoupling: Redis Pub Sub functionality enables decoupling between different components of a system, allowing the components to operate independently and reducing dependencies between different parts of an application.
  • Flexibility: Redis Pub/Sub is a flexible messaging system that can be used for a wide range of use cases, including real-time updates, event-driven architectures, and distributed systems.
  • Reliability: Redis Pub Sub functionality is designed for high availability and reliability, with support for data replication, failover, and recovery.

In summary, Redis Pub/Sub can provide a range of benefits for applications that require real-time messaging and data processing. It enables scalability, real-time communication, decoupling, flexibility, and reliability in distributed systems.

How Does Redis Pub Sub Work?

Redis Pub/Sub works by allowing multiple clients to subscribe to one or more channels and receive messages published to those channels by other clients. The Pub/Sub functionality is built into Redis and uses a publish/subscribe model to enable real-time messaging between different parts of a system.

Here’s how Redis Pub/Sub works:

  1. A client subscribes to a channel using the SUBSCRIBE command.
  2. When another client publishes a message to that channel using the PUBLISH command, Redis delivers the message to all clients that are subscribed to the channel.
  3. Clients can unsubscribe from channels using the UNSUBSCRIBE command.
  4. Redis maintains a list of subscribed channels for each client and delivers messages in real-time to all subscribed clients.

Redis Pub/Sub is designed for high throughput and low latency, making it well-suited for real-time messaging and event-driven architectures.

What is Jedis PubSub?

Jedis PubSub is a feature of the Jedis Java library that provides an implementation of the publish/subscribe messaging paradigm for Redis, an open-source in-memory data structure store.

Pub/Sub allows messages to be broadcast to multiple clients that have subscribed to a particular channel. With Jedis Pub/Sub, clients can subscribe to one or more channels and receive notifications of messages published to those channels.

Jedis PubSub can be used for a variety of purposes, such as real-time messaging, event notification, and task queuing. It is commonly used in web applications, chat systems, and distributed systems that require message broadcasting.

In summary, Jedis Pub/Sub is a feature of the Jedis Java library that enables clients to subscribe to Redis channels and receive real-time notifications of messages published to those channels.

The following example consists of three steps, which we’ll cover next.

Redis Pub Sub Example Step One: Implement the redis.clients.jedis.JedisPubSub Interface

The first step requires that we implement the redis.clients.jedis.JedisPubSub interface. The JedisPubSub specification provides method definitions for subscribing and unsubscribing to channels, and for receiving messages — see lines #6 through #27 in the full example below.

The source code for JedisPubSub can also be found on GitHub.

Redis Pub Sub Example Step Two: Subscribe the JedisPubSub implementation to Jedis

In the second step we need to subscribe the implementation of JedisPubSub to receive messages from Jedis on the updates channel — how this is done can be seen on lines #29 and #34.

Redis Pub Sub Example Step Three: Publish An Example Message On The Updates Channel

In the last step, we send a message to the updates channel via the Redis command line interface (Redis CLI). If all of the components have been configured properly we should see the update appear in the Groovy Console output as demonstrated in the image below.

				
					@GrabResolver(name='Maven Central', root='http://repo1.maven.org/')
@Grab(group='redis.clients', module='jedis', version='3.5.2')
import redis.clients.jedis.Jedis
import redis.clients.jedis.JedisPubSub

class DefaultPubSubImpl extends JedisPubSub {

  @Override
  public void onMessage(String channel, String message) {
    println "channel: $channel, message: $message"
  }

  @Override
  public void onPMessage(String pattern, String channel, String message) {}

  @Override
  public void onSubscribe(String channel, int subscribedChannels) {}

  @Override
  public void onUnsubscribe(String channel, int subscribedChannels) {}

  @Override
  public void onPUnsubscribe(String pattern, int subscribedChannels) {}

  @Override
  public void onPSubscribe(String pattern, int subscribedChannels) {}
}

def subscriber = new DefaultPubSubImpl ()

Jedis jedis = new Jedis("localhost", 6379, 0)
jedis.connect();

jedis.subscribe (subscriber, "updates")
				
			

Example Groovy Script execution demonstrating Redis Pub/Sub with Jedis

In the image below we can see the Groovy script running and printing to the console an update of “Hello world!” which was sent on the updates channel.

On the right-hand side of this image we can also see the “Hello world!” message being sent from the console.

The example Groovy script which relies on Jedis and running in the GroovyConsole with output on a channel which was published from the terminal, which is also shown here.
Example output demonstrating Redis Pub Sub with Jedis in the GroovyConsole

Redis Pub Sub Example Article References

  1. Redis Pub Sub documentation
  2. Redis Pub/Sub definition — see also the section entitled “Pub/Sub best Practices in Redis”
  3. This article was written with assistance from ChatGPT.

thospfuller

I am a Web Design, Technical SEO, and WordPress Specialist based in Northern Virginia. I am interested in software development, content engineering, and business. I'm originally from Chicago, IL, and currently reside in Reston, VA.