Module: Valkey::Commands::PubSubCommands
- Defined in:
- lib/valkey/commands/pubsub_commands.rb
Overview
This module contains commands for Valkey Pub/Sub.
Pub/Sub requires the RESP3 protocol. Subscriptions can be declared when the client is created, and are applied via SUBSCRIBE/PSUBSCRIBE/SSUBSCRIBE during connection establishment:
valkey = Valkey.new(
protocol: :resp3,
pubsub: {
subscriptions: {
exact: ["news"], # channel names
pattern: ["news.*"], # channel name patterns
sharded: ["shard-chan"] # sharded channels, cluster mode only
}
}
)
There are three ways to receive messages:
- Inline: messages are queued on the connection and read with #get_pubsub_message or #try_get_pubsub_message.
- Callback: a
callback:proc receives every message instead, along with an arbitrarycontext:. Not supported yet. - Lazy: the
_lazysubscribe and unsubscribe methods return without waiting for the server to confirm the change; read back #get_subscriptions to see the subscriptions the server actually has.
Instance Method Summary collapse
-
#get_pubsub_message ⇒ Valkey::Glide::PubSub::Message?
Get the next Pub/Sub message, blocking until one is available.
-
#get_subscriptions ⇒ Valkey::Glide::PubSub::SubscriptionState
Get this connection's subscription state: what the client asked for and what the server confirmed.
-
#psubscribe(*patterns, timeout: nil) ⇒ void
Subscribe to channel patterns, waiting for the server to confirm the subscription.
-
#psubscribe_lazy(*patterns) ⇒ void
Subscribe to channel patterns without waiting for the server to confirm.
-
#publish(message, channel, sharded: false) ⇒ Integer
Publish a message on a Pub/Sub channel.
-
#pubsub_channels(pattern = nil) ⇒ Array<String>
List the currently active channels, that is, the channels with at least one subscriber.
-
#pubsub_numpat ⇒ Integer
Get the number of unique patterns that are subscribed to by clients.
-
#pubsub_numsub(*channels) ⇒ Hash{String => Integer}
Get the number of subscribers for the specified channels, exclusive of clients subscribed to patterns.
-
#pubsub_shardchannels(pattern = nil) ⇒ Array<String>
List the currently active sharded channels, that is, the ones with at least one subscriber.
-
#pubsub_shardnumsub(*channels) ⇒ Hash{String => Integer}
Get the number of subscribers for the specified sharded channels, exclusive of clients subscribed to patterns.
-
#punsubscribe(*patterns, timeout: nil) ⇒ void
Unsubscribe from channel patterns, waiting for the server to confirm the change.
-
#punsubscribe_lazy(*patterns) ⇒ void
Unsubscribe from channel patterns without waiting for the server to confirm.
-
#ssubscribe(*channels, timeout: nil) ⇒ void
Subscribe to sharded channels, waiting for the server to confirm the subscription.
-
#ssubscribe_lazy(*channels) ⇒ void
Subscribe to sharded channels without waiting for the server to confirm.
-
#subscribe(*channels, timeout: nil) ⇒ void
Subscribe to exact channels, waiting for the server to confirm the subscription.
-
#subscribe_lazy(*channels) ⇒ void
Subscribe to exact channels without waiting for the server to confirm.
-
#sunsubscribe(*channels, timeout: nil) ⇒ void
Unsubscribe from sharded channels, waiting for the server to confirm the change.
-
#sunsubscribe_lazy(*channels) ⇒ void
Unsubscribe from sharded channels without waiting for the server to confirm.
-
#try_get_pubsub_message ⇒ Valkey::Glide::PubSub::Message?
Get the next Pub/Sub message if one is already queued.
-
#unsubscribe(*channels, timeout: nil) ⇒ void
Unsubscribe from exact channels, waiting for the server to confirm the change.
-
#unsubscribe_lazy(*channels) ⇒ void
Unsubscribe from exact channels without waiting for the server to confirm.
Instance Method Details
#get_pubsub_message ⇒ Valkey::Glide::PubSub::Message?
Get the next Pub/Sub message, blocking until one is available.
306 307 308 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 306 def @pubsub. end |
#get_subscriptions ⇒ Valkey::Glide::PubSub::SubscriptionState
Get this connection's subscription state: what the client asked for and what the server confirmed.
337 338 339 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 337 def get_subscriptions @pubsub.get_subscriptions end |
#psubscribe(*patterns, timeout: nil) ⇒ void
This method returns an undefined value.
Subscribe to channel patterns, waiting for the server to confirm the subscription.
91 92 93 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 91 def psubscribe(*patterns, timeout: nil) @pubsub.psubscribe(*patterns, timeout: timeout) end |
#psubscribe_lazy(*patterns) ⇒ void
This method returns an undefined value.
Subscribe to channel patterns without waiting for the server to confirm.
The client subscribes asynchronously in the background.
210 211 212 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 210 def psubscribe_lazy(*patterns) @pubsub.psubscribe_lazy(*patterns) end |
#publish(message, channel, sharded: false) ⇒ Integer
Publish a message on a Pub/Sub channel.
This method aggregates the PUBLISH and SPUBLISH functionalities; the mode is selected with the
sharded parameter. In both modes the request is routed using the hashed channel as key. The
argument order is (message, channel), matching the other GLIDE clients.
293 294 295 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 293 def publish(, channel, sharded: false) @pubsub.publish(, channel, sharded: sharded) end |
#pubsub_channels(pattern = nil) ⇒ Array<String>
List the currently active channels, that is, the channels with at least one subscriber.
In cluster mode the command is routed to all nodes and the responses are combined.
358 359 360 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 358 def pubsub_channels(pattern = nil) @pubsub.pubsub_channels(pattern) end |
#pubsub_numpat ⇒ Integer
Get the number of unique patterns that are subscribed to by clients.
In cluster mode the command is routed to all nodes and the counts are summed.
This is the total number of unique patterns all the clients are subscribed to, not the count of clients subscribed to patterns.
377 378 379 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 377 def pubsub_numpat @pubsub.pubsub_numpat end |
#pubsub_numsub(*channels) ⇒ Hash{String => Integer}
Get the number of subscribers for the specified channels, exclusive of clients subscribed to patterns.
In cluster mode the command is routed to all nodes and the counts are combined.
398 399 400 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 398 def pubsub_numsub(*channels) @pubsub.pubsub_numsub(*channels) end |
#pubsub_shardchannels(pattern = nil) ⇒ Array<String>
List the currently active sharded channels, that is, the ones with at least one subscriber.
In cluster mode the command is routed to all nodes and the responses are combined.
Only available in cluster mode (cluster_mode: true). Since: Valkey version 7.0.0.
421 422 423 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 421 def pubsub_shardchannels(pattern = nil) @pubsub.pubsub_shardchannels(pattern) end |
#pubsub_shardnumsub(*channels) ⇒ Hash{String => Integer}
Get the number of subscribers for the specified sharded channels, exclusive of clients subscribed to patterns.
Only available in cluster mode (cluster_mode: true). Since: Valkey version 7.0.0.
443 444 445 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 443 def pubsub_shardnumsub(*channels) @pubsub.pubsub_shardnumsub(*channels) end |
#punsubscribe(*patterns, timeout: nil) ⇒ void
This method returns an undefined value.
Unsubscribe from channel patterns, waiting for the server to confirm the change.
112 113 114 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 112 def punsubscribe(*patterns, timeout: nil) @pubsub.punsubscribe(*patterns, timeout: timeout) end |
#punsubscribe_lazy(*patterns) ⇒ void
This method returns an undefined value.
Unsubscribe from channel patterns without waiting for the server to confirm.
227 228 229 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 227 def punsubscribe_lazy(*patterns) @pubsub.punsubscribe_lazy(*patterns) end |
#ssubscribe(*channels, timeout: nil) ⇒ void
This method returns an undefined value.
Subscribe to sharded channels, waiting for the server to confirm the subscription.
Only available in cluster mode (cluster_mode: true). Since: Valkey version 7.0.0.
134 135 136 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 134 def ssubscribe(*channels, timeout: nil) @pubsub.ssubscribe(*channels, timeout: timeout) end |
#ssubscribe_lazy(*channels) ⇒ void
This method returns an undefined value.
Subscribe to sharded channels without waiting for the server to confirm.
The client subscribes asynchronously in the background. Only available in cluster mode
(cluster_mode: true). Since: Valkey version 7.0.0.
246 247 248 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 246 def ssubscribe_lazy(*channels) @pubsub.ssubscribe_lazy(*channels) end |
#subscribe(*channels, timeout: nil) ⇒ void
This method returns an undefined value.
Subscribe to exact channels, waiting for the server to confirm the subscription.
50 51 52 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 50 def subscribe(*channels, timeout: nil) @pubsub.subscribe(*channels, timeout: timeout) end |
#subscribe_lazy(*channels) ⇒ void
This method returns an undefined value.
Subscribe to exact channels without waiting for the server to confirm.
The client subscribes asynchronously in the background.
175 176 177 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 175 def subscribe_lazy(*channels) @pubsub.subscribe_lazy(*channels) end |
#sunsubscribe(*channels, timeout: nil) ⇒ void
This method returns an undefined value.
Unsubscribe from sharded channels, waiting for the server to confirm the change.
Only available in cluster mode (cluster_mode: true). Since: Valkey version 7.0.0.
157 158 159 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 157 def sunsubscribe(*channels, timeout: nil) @pubsub.sunsubscribe(*channels, timeout: timeout) end |
#sunsubscribe_lazy(*channels) ⇒ void
This method returns an undefined value.
Unsubscribe from sharded channels without waiting for the server to confirm.
Only available in cluster mode (cluster_mode: true). Since: Valkey version 7.0.0.
265 266 267 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 265 def sunsubscribe_lazy(*channels) @pubsub.sunsubscribe_lazy(*channels) end |
#try_get_pubsub_message ⇒ Valkey::Glide::PubSub::Message?
Get the next Pub/Sub message if one is already queued. Never blocks.
321 322 323 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 321 def @pubsub. end |
#unsubscribe(*channels, timeout: nil) ⇒ void
This method returns an undefined value.
Unsubscribe from exact channels, waiting for the server to confirm the change.
71 72 73 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 71 def unsubscribe(*channels, timeout: nil) @pubsub.unsubscribe(*channels, timeout: timeout) end |
#unsubscribe_lazy(*channels) ⇒ void
This method returns an undefined value.
Unsubscribe from exact channels without waiting for the server to confirm.
192 193 194 |
# File 'lib/valkey/commands/pubsub_commands.rb', line 192 def unsubscribe_lazy(*channels) @pubsub.unsubscribe_lazy(*channels) end |