How to Use Valkey Pub/Sub (Publish/Subscribe) in Java?

The publish/subscribe (pub/sub) messaging pattern provides the foundation for today's high-performance distributed applications. Several different tools are available to serve as a pub/sub message broker, but developers are increasingly turning toward the in-memory data store Valkey.

As the open-source fork of the popular Redis database, Valkey offers blazing-fast performance and strong community support. For Java developers, however, there are only a couple of options if they also want to use Valkey and its pub/sub features. Since Valkey doesn't natively support Java, developers must either program their own interface to Valkey's PUBLISH and SUBSCRIBE commands or use a third-party Valkey client. The Redisson client and its more advanced Redisson PRO version provide Java developers with the easiest and most feature-rich solutions. Here's what you need to know about how to use Valkey pub/sub in Java.

What Is Pub/Sub in Valkey? And How Does It Work?

Pub/sub is an asynchronous communication pattern built around four key concepts.

The first concept is the message,  a self-contained packet of data. In the pub/sub pattern, a publisher is responsible for sending messages, while the subscriber is the entity that receives messages from one or more publishers.

The last (but perhaps most important) pub/sub concept is the topic. In the world of pub/sub, a topic is essentially a channel that categorizes messages by subject. Publishers can send messages to a topic without worrying about which or how many subscribers are listening. On the other hand, subscribers can express interest in any given topic and receive messages without knowing the publisher's identity. 

This decoupling between publishers and subscribers makes topics a flexible model for developers. When combined with Valkey's fast in-memory performance, the pub/sub model is ideal for the most demanding applications developers create today.

For example, pub/sub is perfect for real-time messaging in chat apps and social media. When a user sends a message, it is published to a topic representing a specific feed. All subscribers to the topic/feed instantly receive the message — which could be comments, likes, or any other interactions that take place within the applications.

Another popular application of pub/sub is streaming video. In a streaming app, chunks of video and audio can be published as messages to a topic. Viewers subscribe to the topic to receive the streaming video. 

These are just a couple of examples. Messages and topics in the pub/sub paradigm provide scalability and flexibility to countless other apps, with Valkey serving as the message broker.

Using Redisson for Pub/Sub With Valkey in Java Applications

Thanks to its multiple topic implementations, Redisson makes it simple to set up distributed pub/sub with Valkey. The three types of topics supported by Redisson are available as objects that will be familiar to any Java developer. Each object takes care of everything on the Valkey server, so developers don't need to code their own pub/sub solutions.

Basic Topic

A basic topic is implemented with Redisson's RTopic object. RTopic allows a Java app to subscribe to events published with multiple instances of the object with the same name.

Here's a code sample so you can see how easy it is to use RTopic:

RTopic topic = redisson.getTopic("myTopic");
int listenerId = topic.addListener(SomeObject.class, new MessageListener() {
  @Override
  public void onMessage(CharSequence channel, SomeObject message) {
    //...
  }
});


// in other thread or JVM
RTopic topic = redisson.getTopic("myTopic");
long clientsReceivedMessage = topic.publish(new SomeObject());

Sharded Topic

Redisson's RShardedTopic object works with Valkey's implementation of sharded topics across clusters. RShardedTopic works similar to RTopic:

RShardedTopic topic = redisson.getShardedTopic("myTopic");
int listenerId = topic.addListener(SomeObject.class, new MessageListener() {
  @Override
  public void onMessage(CharSequence channel, SomeObject message) {
    //...
  }
});


// in other thread or JVM
RShardedTopic topic = redisson.getShardedTopic("myTopic");
long clientsReceivedMessage = topic.publish(new SomeObject());

Reliable Topic

Reidsson also supports the reliable topic, ensuring messages are delivered despite temporary network issues or service disruptions. Valkey does not support this feature out of the box; it is only available with Redisson or Redisson PRO.

The RReliableTopic object works much the same way as Redisson's other topic implementations:

RReliableTopic topic = redisson.getReliableTopic("anyTopic");
topic.addListener(SomeObject.class, new MessageListener() {
  @Override
  public void onMessage(CharSequence channel, SomeObject message) {
    //...
  }
});


// in other thread or JVM
RReliableTopic topic = redisson.getReliableTopic("anyTopic");
long subscribersReceivedMessage = topic.publish(new SomeObject());

Redisson, the Java Developer's Choice for Using Valkey Pub/Sub

Pub/sub provides asynchronous communication while supporting scalability and flexibility in today's high-performance distributed applications. Valkey is the open-source, in-memory data store developers prefer as a pub/sub message broker. Java developers just need a client to make it happen, and that's where Redisson and Redisson PRO come in.

Redisson and Redisson PRO's simple Java objects make it easy for developers to implement Valkey pub/sub in their distributed applications. Now it only takes a few lines of code to implement basic topics, sharded topics, and even reliable topics in Java apps. Pub/sub is just one of many features offered by Redisson and Redisson PRO for Java developers. To learn more, visit the Redisson PRO website today. 

Similar articles