module Restforce::Concerns::Streaming

Public Instance Methods

faye() click to toggle source

Public: Faye client to use for subscribing to PushTopics

# File lib/restforce/concerns/streaming.rb, line 33
def faye
  unless options[:instance_url]
    raise 'Instance URL missing. Call .authenticate! first.'
  end

  url = "#{options[:instance_url]}/cometd/#{options[:api_version]}"

  @faye ||= Faye::Client.new(url).tap do |client|
    client.set_header 'Authorization', "OAuth #{options[:oauth_token]}"

    client.bind 'transport:down' do
      Restforce.log "[COMETD DOWN]"
      client.set_header 'Authorization', "OAuth #{authenticate!.access_token}"
    end

    client.bind 'transport:up' do
      Restforce.log "[COMETD UP]"
    end

    client.add_extension ReplayExtension.new(replay_handlers)
  end
end
legacy_subscribe(topics, options = {}, &block) click to toggle source

Public: Subscribe to a PushTopic

topics - The name of the PushTopic channel(s) to subscribe to. block - A block to run when a new message is received.

Returns a Faye::Subscription

# File lib/restforce/concerns/streaming.rb, line 12
def legacy_subscribe(topics, options = {}, &block)
  topics = Array(topics).map { |channel| "/topic/#{channel}" }
  subscription(topics, options, &block)
end
Also aliased as: subscribe
replay_handlers() click to toggle source
# File lib/restforce/concerns/streaming.rb, line 56
def replay_handlers
  @_replay_handlers ||= {}
end
subscribe(topics, options = {}, &block)
Alias for: legacy_subscribe
subscription(channels, options = {}, &block) click to toggle source

Public: Subscribe to one or more Streaming API channels

channels - The name of the Streaming API (cometD) channel(s) to subscribe to. block - A block to run when a new message is received.

Returns a Faye::Subscription

# File lib/restforce/concerns/streaming.rb, line 24
def subscription(channels, options = {}, &block)
  one_or_more_channels = Array(channels)
  one_or_more_channels.each do |channel|
    replay_handlers[channel] = options[:replay]
  end
  faye.subscribe(one_or_more_channels, &block)
end