class

Redis::Subscription

Inherits Reference < Object

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:

Instance methods

close
Source
on_message

Define a callback for when a new message is received.

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

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

Source
on_unsubscribe

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

Source

Nested types