class Redis::Subscription

Overview

The Subscription is what is yielded to a Connection#subscribe block. It is used to setup callbacks when messages come in, the connection is subscribed to other channels or patterns, or unsubscribed from any channels or patterns.

redis.subscribe "channel1", "channel2" do |subscription, connection|
  subscription.on_message do |channel, message|
    if message == "unsubscribe"
      connection.unsubscribe channel
    end

    # ...
  end

  # Respond to new subscribers
  subscription.on_subscribe do |channel, sub_count|
    connection.incr "sub_count:#{channel}"
  end

  # Respond to losing subscribers
  subscription.on_unsubscribe do |channel, sub_count|
    connection.incr "sub_count:#{channel}"
  end
end

For more information, see the documentation for:

Defined in:

connection.cr

Instance Method Summary

Instance Method Detail

def close #

[View source]
def on_message(&on_message : String, String, String -> ) #

Define a callback for when a new message is received.

subscription.on_message do |channel, message|
  pp channel: channel, message: message
end

[View source]
def on_subscribe(&on_subscribe : String, Int64 -> ) #

Define a callback to execute when the connection is subscribed to another channel.


[View source]
def on_unsubscribe(&on_unsubscribe : String, Int64 -> ) #

Define a callback to execute when the connection is unsubscribed from another channel.


[View source]