Skip to content

Configure Pub/Sub

Valkey GLIDE PubSub aims to unify various nuances into a coherent interface, minimizing differences between Sharded, Cluster, and Standalone configurations. Additionally, GLIDE is responsible for tracking topology changes in real time, ensuring the client remains subscribed regardless of any connectivity issues.

This guide will go over how to configure GLIDE pubsub system.

Due to the implementation of the resubscription logic, it is recommended to use a dedicated client for PubSub subscriptions. That is, the client with subscriptions should not be the same client that issues commands. In case of topology changes, the internal connections might be reestablished to resubscribe to the correct servers.

To publish a message, create a separate client to your Valkey instance. Both Standalone and Cluster are supported.

// Publishing is done with a separate client
GlideClientConfiguration publishConfig = GlideClientConfiguration.builder()
.address(NodeAddress.builder().port(6379).build())
.build();
try (var publishingClient = GlideClient.createClient(publishConfig).get()) {
// publish message on ch1 channel
publishingClient.publish("Test message", "ch1").get();
}

To subscribe to messages, use a separate client with subscriptions configured through the client’s configuration object.

Glide clients can register a callback method to handle subscriptions in the client configuration.

MessageCallback callback =
(msg, context) -> System.out.printf("Received %s, context %s\n", msg, context);
GlideClientConfiguration config = GlideClientConfiguration.builder()
.address(NodeAddress.builder().port(6379).build())
.requestTimeout(3000)
// subscriptions are configured here
.subscriptionConfiguration(StandaloneSubscriptionConfiguration.builder()
.subscription(EXACT, "ch1") // Listens for messages published to 'ch1' channel, in unsharded mode
.subscription(EXACT, "ch2") // Listens for messages published to 'ch2' channel, in unsharded mode
.subscription(PATTERN, "chat*") // Listens for messages published to channels matched by 'chat*' glob pattern, in unsharded mode
.callback(callback)
.callback(callback, context) // callback or callback with context are configured here
.build())
.build();
try (var regularClient = GlideClient.createClient(config).get()) {
// Do some work/wait - the callbacks will be dispatched on incoming messages
} // unsubscribe happens here

Alternatively, GLIDE clients also provide options to use the client’s native async features to handle polling for messages.

GlideClientConfiguration config = GlideClientConfiguration.builder()
.address(NodeAddress.builder().port(6379).build())
.requestTimeout(3000)
// subscriptions are configured here
.subscriptionConfiguration(StandaloneSubscriptionConfiguration.builder()
.subscription(EXACT, Set.of("ch1", "ch2")) // there is option to set multiple subscriptions at a time
.subscription(Map.of(PATTERN, "chat*", EXACT, Set.of("ch1", "ch2")))
// or even all subscriptions at a time
.build()) // no callback is configured
.build()
try (var regularClient = GlideClient.createClient(config).get()) {
Message msg = regularClient.tryGetPubSubMessage(); // sync, does not block
Message msg = regularClient.getPubSubMessage().get(); // async, waits for the next message
} // unsubscribe happens here

To learn more about GLIDE’s PubSub model, see our explanation.