Module: Valkey::Commands::PubSubCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/pubsub_commands.rb

Overview

This module contains commands related to Valkey Pub/Sub.

Instance Method Summary collapse

Instance Method Details

#psubscribe(*patterns) ⇒ String

Subscribe to one or more patterns.

Examples:

Subscribe to patterns

valkey.psubscribe("news.*", "events.*")
  # => "OK"

Parameters:

  • patterns (Array<String>)

    the patterns to subscribe to

Returns:

  • (String)

    “OK”

See Also:



51
52
53
# File 'lib/valkey/commands/pubsub_commands.rb', line 51

def psubscribe(*patterns)
  send_command(RequestType::PSUBSCRIBE, patterns)
end

#publish(channel, message) ⇒ Integer

Publish a message to a channel.

Examples:

Publish a message

valkey.publish("channel1", "Hello, World!")
  # => 2

Parameters:

  • channel (String)

    the channel to publish to

  • message (String)

    the message to publish

Returns:

  • (Integer)

    the number of clients that received the message

See Also:



83
84
85
# File 'lib/valkey/commands/pubsub_commands.rb', line 83

def publish(channel, message)
  send_command(RequestType::PUBLISH, [channel, message])
end

#pubsub(subcommand, *args) ⇒ Object

Control pub/sub operations (convenience method).

Examples:

List active channels

valkey.pubsub(:channels)
  # => ["channel1", "channel2"]

Get pattern count

valkey.pubsub(:numpat)
  # => 3

Get subscriber counts

valkey.pubsub(:numsub, "channel1", "channel2")
  # => ["channel1", 5, "channel2", 3]

List active shard channels

valkey.pubsub(:shardchannels)
  # => ["shard1", "shard2"]

Get shard subscriber counts

valkey.pubsub(:shardnumsub, "shard1", "shard2")
  # => ["shard1", 2, "shard2", 1]

Parameters:

  • subcommand (String, Symbol)

    the subcommand (channels, numpat, numsub, shardchannels, shardnumsub)

  • args (Array)

    arguments for the subcommand

Returns:

  • (Object)

    depends on subcommand



231
232
233
234
# File 'lib/valkey/commands/pubsub_commands.rb', line 231

def pubsub(subcommand, *args)
  subcommand = subcommand.to_s.downcase
  send("pubsub_#{subcommand}", *args)
end

#pubsub_channels(pattern = nil) ⇒ Array<String>

List active channels.

Examples:

List all active channels

valkey.pubsub_channels
  # => ["channel1", "channel2"]

List active channels matching a pattern

valkey.pubsub_channels("news.*")
  # => ["news.sports", "news.tech"]

Parameters:

  • pattern (String) (defaults to: nil)

    optional pattern to filter channels

Returns:

  • (Array<String>)

    list of active channels

See Also:



146
147
148
149
# File 'lib/valkey/commands/pubsub_commands.rb', line 146

def pubsub_channels(pattern = nil)
  args = pattern ? [pattern] : []
  send_command(RequestType::PUBSUB_CHANNELS, args)
end

#pubsub_numpatInteger

Get the number of unique patterns subscribed to.

Examples:

Get pattern count

valkey.pubsub_numpat
  # => 3

Returns:

  • (Integer)

    the number of patterns

See Also:



160
161
162
# File 'lib/valkey/commands/pubsub_commands.rb', line 160

def pubsub_numpat
  send_command(RequestType::PUBSUB_NUM_PAT)
end

#pubsub_numsub(*channels) ⇒ Array

Get the number of subscribers for channels.

Examples:

Get subscriber counts

valkey.pubsub_numsub("channel1", "channel2")
  # => ["channel1", 5, "channel2", 3]

Parameters:

  • channels (Array<String>)

    the channels to check

Returns:

  • (Array)

    channel names and subscriber counts

See Also:



174
175
176
# File 'lib/valkey/commands/pubsub_commands.rb', line 174

def pubsub_numsub(*channels)
  send_command(RequestType::PUBSUB_NUM_SUB, channels)
end

#pubsub_shardchannels(pattern = nil) ⇒ Array<String>

List active shard channels.

Examples:

List all active shard channels

valkey.pubsub_shardchannels
  # => ["shard1", "shard2"]

List active shard channels matching a pattern

valkey.pubsub_shardchannels("shard.*")
  # => ["shard.1", "shard.2"]

Parameters:

  • pattern (String) (defaults to: nil)

    optional pattern to filter shard channels

Returns:

  • (Array<String>)

    list of active shard channels

See Also:



191
192
193
194
# File 'lib/valkey/commands/pubsub_commands.rb', line 191

def pubsub_shardchannels(pattern = nil)
  args = pattern ? [pattern] : []
  send_command(RequestType::PUBSUB_SHARD_CHANNELS, args)
end

#pubsub_shardnumsub(*channels) ⇒ Array

Get the number of subscribers for shard channels.

Examples:

Get shard subscriber counts

valkey.pubsub_shardnumsub("shard1", "shard2")
  # => ["shard1", 2, "shard2", 1]

Parameters:

  • channels (Array<String>)

    the shard channels to check

Returns:

  • (Array)

    shard channel names and subscriber counts

See Also:



206
207
208
# File 'lib/valkey/commands/pubsub_commands.rb', line 206

def pubsub_shardnumsub(*channels)
  send_command(RequestType::PUBSUB_SHARD_NUM_SUB, channels)
end

#punsubscribe(*patterns) ⇒ String

Unsubscribe from one or more patterns.

Examples:

Unsubscribe from patterns

valkey.punsubscribe("news.*", "events.*")
  # => "OK"

Unsubscribe from all patterns

valkey.punsubscribe
  # => "OK"

Parameters:

  • patterns (Array<String>)

    the patterns to unsubscribe from (empty for all)

Returns:

  • (String)

    “OK”

See Also:



68
69
70
# File 'lib/valkey/commands/pubsub_commands.rb', line 68

def punsubscribe(*patterns)
  send_command(RequestType::PUNSUBSCRIBE, patterns)
end

#spublish(channel, message) ⇒ Integer

Publish a message to a shard channel.

Examples:

Publish a message to a shard channel

valkey.spublish("shard1", "Hello, Shard!")
  # => 1

Parameters:

  • channel (String)

    the shard channel to publish to

  • message (String)

    the message to publish

Returns:

  • (Integer)

    the number of clients that received the message

See Also:



129
130
131
# File 'lib/valkey/commands/pubsub_commands.rb', line 129

def spublish(channel, message)
  send_command(RequestType::SPUBLISH, [channel, message])
end

#ssubscribe(*channels) ⇒ String

Subscribe to one or more shard channels.

Examples:

Subscribe to shard channels

valkey.ssubscribe("shard1", "shard2")
  # => "OK"

Parameters:

  • channels (Array<String>)

    the shard channels to subscribe to

Returns:

  • (String)

    “OK”

See Also:



97
98
99
# File 'lib/valkey/commands/pubsub_commands.rb', line 97

def ssubscribe(*channels)
  send_command(RequestType::SSUBSCRIBE, channels)
end

#subscribe(*channels) ⇒ String

Subscribe to one or more channels.

Examples:

Subscribe to channels

valkey.subscribe("channel1", "channel2")
  # => "OK"

Parameters:

  • channels (Array<String>)

    the channels to subscribe to

Returns:

  • (String)

    “OK”

See Also:



20
21
22
# File 'lib/valkey/commands/pubsub_commands.rb', line 20

def subscribe(*channels)
  send_command(RequestType::SUBSCRIBE, channels)
end

#sunsubscribe(*channels) ⇒ String

Unsubscribe from one or more shard channels.

Examples:

Unsubscribe from shard channels

valkey.sunsubscribe("shard1", "shard2")
  # => "OK"

Unsubscribe from all shard channels

valkey.sunsubscribe
  # => "OK"

Parameters:

  • channels (Array<String>)

    the shard channels to unsubscribe from (empty for all)

Returns:

  • (String)

    “OK”

See Also:



114
115
116
# File 'lib/valkey/commands/pubsub_commands.rb', line 114

def sunsubscribe(*channels)
  send_command(RequestType::SUNSUBSCRIBE, channels)
end

#unsubscribe(*channels) ⇒ String

Unsubscribe from one or more channels.

Examples:

Unsubscribe from channels

valkey.unsubscribe("channel1", "channel2")
  # => "OK"

Unsubscribe from all channels

valkey.unsubscribe
  # => "OK"

Parameters:

  • channels (Array<String>)

    the channels to unsubscribe from (empty for all)

Returns:

  • (String)

    “OK”

See Also:



37
38
39
# File 'lib/valkey/commands/pubsub_commands.rb', line 37

def unsubscribe(*channels)
  send_command(RequestType::UNSUBSCRIBE, channels)
end