module Pubsub
Provides Redis Pub/Sub commands for cluster environments. Uses sharded pub/sub by default for optimal cluster performance.
Definitions
def publish(channel, message, role: :master)
Post a message to a channel using cluster-optimized sharded publish. Routes the message directly to the appropriate shard based on the channel name.
Signature
-
parameter
channelString The channel name.
-
parameter
messageString The message to publish.
-
parameter
roleSymbol The role of node to use (
:masteror:slave).-
returns
Integer The number of clients that received the message.
Implementation
def publish(channel, message, role: :master)
# Route to the correct shard based on channel name:
slot = slot_for(channel)
client = client_for(slot, role)
return client.call("SPUBLISH", channel, message)
end