class Attention::BlockingSubscriber

Uses Redis pub/sub to synchronously respond to events

Each Subscriber uses a Redis connection to listen to a channel for events.

Attributes

channel[R]

The channel subscribed to

redis[R]

@!visibility private

Public Class Methods

new(channel, &callback) click to toggle source

Creates a subscription to the given channel @param channel [String] The channel to listen to @yield The code to execute on a published event @yieldparam channel [String] The channel the subscriber is listening to @yieldparam data [Object] The event published on the channel @yieldparam subscriber [BlockingSubscriber] This instance

# File lib/attention/blocking_subscriber.rb, line 22
def initialize(channel, &callback)
  @channel = channel
  @redis = Connection.new
  subscribe &callback
end

Public Instance Methods

publisher() click to toggle source

The {Publisher} used to send the unsubscribe message @api private

# File lib/attention/blocking_subscriber.rb, line 41
def publisher
  @publisher ||= Publisher.new
end
subscribe(&callback) click to toggle source

Sets up the Redis pub/sub subscription @yield The code to execute on a published event

# File lib/attention/blocking_subscriber.rb, line 30
def subscribe(&callback)
  redis.subscribe(channel) do |on|
    on.message do |channel, payload|
      data = JSON.parse(payload) rescue payload
      callback.call channel, data, self
    end
  end
end
unsubscribe() click to toggle source

Unsubscribes from the channel

# File lib/attention/blocking_subscriber.rb, line 46
def unsubscribe
  redis.unsubscribe
end