public interface StatefulRedisClusterPubSubConnection<K,V> extends StatefulRedisPubSubConnection<K,V>
This connection allows publishing and subscription to Pub/Sub messages within a Redis Cluster. Due to Redis Cluster's nature, messages are broadcasted across the cluster and a client can connect to any arbitrary node to participate with a subscription.
StatefulRedisClusterPubSubConnection<String, String> connection = clusterClient.connectPubSub();
connection.addListener(…);
RedisClusterPubSubCommands<String, String> sync = connection.sync();
sync.subscribe("channel");
sync.publish("channel", "message");
StatefulRedisClusterPubSubConnection allows node-specific subscriptions and message
propagation. setNodeMessagePropagation(boolean) can notify a RedisPubSubListener that requires a single registration
with this connection. Node-subscriptions are supported on
connection and NodeSelection levels through
asynchronous, synchronous, and reactive APIs.
StatefulRedisClusterPubSubConnection<String, String> connection = clusterClient.connectPubSub();
connection.addListener(…);
RedisClusterPubSubCommands<String, String> sync = connection.sync();
sync.replicas().commands().psubscribe("__key*__:*");
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(RedisClusterPubSubListener<K,V> listener)
Add a new
listener. |
void |
addListener(RedisClusterPushListener listener)
Add a new
listener to consume push messages. |
RedisClusterPubSubAsyncCommands<K,V> |
async()
Returns the
RedisClusterPubSubAsyncCommands API for the current connection. |
default <T> T |
commands(ClusterPubSubCommandsFactory<StatefulRedisClusterPubSubConnection<K,V>,T> factory)
Returns the command API created by
factory, bound to this connection. |
StatefulRedisPubSubConnection<K,V> |
getConnection(String nodeId)
Retrieve a connection to the specified cluster node using the nodeId.
|
StatefulRedisPubSubConnection<K,V> |
getConnection(String host,
int port)
Retrieve a connection to the specified cluster node using host and port.
|
CompletableFuture<StatefulRedisPubSubConnection<K,V>> |
getConnectionAsync(String nodeId)
Retrieve asynchronously a connection to the specified cluster node using the nodeId.
|
CompletableFuture<StatefulRedisPubSubConnection<K,V>> |
getConnectionAsync(String host,
int port)
Retrieve a connection to the specified cluster node using host and port.
|
Partitions |
getPartitions() |
RedisClusterPubSubReactiveCommands<K,V> |
reactive()
Deprecated.
since 7.8, use
commands(ClusterPubSubCommandsFactory) with
RedisClusterPubSubReactiveCommands.factory() instead; scheduled for removal in a future major
release. |
void |
removeListener(RedisClusterPubSubListener<K,V> listener)
Remove an existing
listener. |
void |
removeListener(RedisClusterPushListener listener)
Remove an existing
listener. |
void |
setNodeMessagePropagation(boolean enabled)
|
RedisClusterPubSubCommands<K,V> |
sync()
Returns the
RedisClusterPubSubCommands API for the current connection. |
addListener, commands, removeListeneraddListener, commands, isMulti, removeListeneraddListener, close, closeAsync, dispatch, dispatch, flushCommands, getCodec, getOptions, getResources, getTimeout, isOpen, removeListener, setAutoFlushCommands, setTimeoutRedisClusterPubSubCommands<K,V> sync()
RedisClusterPubSubCommands API for the current connection. Does not create a new connection.
Note: this accessor is scheduled for removal in a future major release; a commands(...)-based replacement is
planned.
sync in interface StatefulRedisConnection<K,V>sync in interface StatefulRedisPubSubConnection<K,V>RedisClusterPubSubAsyncCommands<K,V> async()
RedisClusterPubSubAsyncCommands API for the current connection. Does not create a new connection.
Note: this accessor is scheduled for removal in a future major release; a commands(...)-based replacement is
planned.
async in interface StatefulRedisConnection<K,V>async in interface StatefulRedisPubSubConnection<K,V>@Deprecated RedisClusterPubSubReactiveCommands<K,V> reactive()
commands(ClusterPubSubCommandsFactory) with
RedisClusterPubSubReactiveCommands.factory() instead; scheduled for removal in a future major
release.RedisClusterPubSubReactiveCommands API for the current connection. Does not create a new connection.reactive in interface StatefulRedisConnection<K,V>reactive in interface StatefulRedisPubSubConnection<K,V>StatefulRedisPubSubConnection<K,V> getConnection(String nodeId)
nodeId, that behavior can also lead to a closed connection once the node with the
specified nodeId is no longer part of the cluster.
Do not close the connections. Otherwise, unpredictable behavior will occur. The nodeId must be part of the cluster and is
validated against the current topology view in Partitions.
nodeId - the node IdRedisException - if the requested node identified by nodeId is not part of the clusterCompletableFuture<StatefulRedisPubSubConnection<K,V>> getConnectionAsync(String nodeId)
nodeId, that behavior can also lead to a closed connection once the
node with the specified nodeId is no longer part of the cluster.
Do not close the connections. Otherwise, unpredictable behavior will occur. The nodeId must be part of the cluster and is
validated against the current topology view in Partitions.
nodeId - the node IdCompletableFuture to indicate success or failure to connect to the requested cluster node.RedisException - if the requested node identified by nodeId is not part of the clusterStatefulRedisPubSubConnection<K,V> getConnection(String host, int port)
host and port,
are no longer part of the cluster.
Do not close the connections. Otherwise, unpredictable behavior will occur. Host and port connections are verified by
default for cluster membership, see ClusterClientOptions.isValidateClusterNodeMembership().
host - the hostport - the portRedisException - if the requested node identified by host and port is not part of the clusterCompletableFuture<StatefulRedisPubSubConnection<K,V>> getConnectionAsync(String host, int port)
host and port,
are no longer part of the cluster.
Do not close the connections. Otherwise, unpredictable behavior will occur. Host and port connections are verified by
default for cluster membership, see ClusterClientOptions.isValidateClusterNodeMembership().
host - the hostport - the portCompletableFuture to indicate success or failure to connect to the requested cluster node.RedisException - if the requested node identified by host and port is not part of the clusterPartitions getPartitions()
void setNodeMessagePropagation(boolean enabled)
this connections
listeners.
If enabled is true, then Pub/Sub messages received on node-specific connections are propagated to this
connection facade. Registered RedisPubSubListener will receive messages from individual node subscriptions.
Node event propagation is disabled by default.
enabled - true to enable node message propagation; false (default) to disable message propagation.void addListener(RedisClusterPubSubListener<K,V> listener)
listener.listener - the listener, must not be null.void removeListener(RedisClusterPubSubListener<K,V> listener)
listener.listener - the listener, must not be null.void addListener(RedisClusterPushListener listener)
listener to consume push messages.listener - the listener, must not be null.void removeListener(RedisClusterPushListener listener)
listener.listener - the listener, must not be null.default <T> T commands(ClusterPubSubCommandsFactory<StatefulRedisClusterPubSubConnection<K,V>,T> factory)
factory, bound to this connection. Does not create a new connection.
The command API is created once per connection and cached; calling this method again with the same factory
returns the same instance.
T - the command API typefactory - the command API factory, must not be nullUnsupportedOperationException - if the connection implementation does not override this method. The default is
provided only for source compatibility in Lettuce 7.x and becomes an abstract method in Lettuce 8.0.Copyright © 2026 lettuce.io. All rights reserved.