class Slanger::Channel

Attributes

channel_id[R]

Public Class Methods

all() click to toggle source
# File lib/slanger/channel.rb, line 36
def all
  @all ||= {}
end
create(params = {}) click to toggle source
# File lib/slanger/channel.rb, line 30
def create(params = {})
  new(params).tap do |channel|
    all[channel.channel_id] = channel
  end
end
from(channel_id) click to toggle source
# File lib/slanger/channel.rb, line 20
def from(channel_id)
  klass = channel_id[/\Apresence-/] ? PresenceChannel : Channel

  klass.lookup(channel_id) || klass.create(channel_id: channel_id)
end
lookup(channel_id) click to toggle source
# File lib/slanger/channel.rb, line 26
def lookup(channel_id)
  all[channel_id]
end
new(attrs) click to toggle source
# File lib/slanger/channel.rb, line 49
def initialize(attrs)
  @channel_id = attrs.with_indifferent_access[:channel_id]
  Slanger::Redis.subscribe channel_id
end
send_client_message(msg) click to toggle source
# File lib/slanger/channel.rb, line 44
def send_client_message(msg)
  from(msg["channel"]).try :send_client_message, msg
end
unsubscribe(channel_id, subscription_id) click to toggle source
# File lib/slanger/channel.rb, line 40
def unsubscribe(channel_id, subscription_id)
  from(channel_id).try :unsubscribe, subscription_id
end

Public Instance Methods

authenticated?() click to toggle source
# File lib/slanger/channel.rb, line 91
def authenticated?
  channel_id =~ /\Aprivate-/ || channel_id =~ /\Apresence-/
end
channel() click to toggle source
# File lib/slanger/channel.rb, line 54
def channel
  @channel ||= EM::Channel.new
end
dispatch(message, channel) click to toggle source

Send an event received from Redis to the EventMachine channel which will send it to subscribed clients.

# File lib/slanger/channel.rb, line 85
def dispatch(message, channel)
  push(Oj.dump(message, mode: :compat)) unless channel =~ /\Aslanger:/

  perform_client_webhook!(message)
end
send_client_message(message) click to toggle source

Send a client event to the EventMachine channel. Only events to channels requiring authentication (private or presence) are accepted. Public channels only get events from the API.

# File lib/slanger/channel.rb, line 79
def send_client_message(message)
  Slanger::Redis.publish(message["channel"], Oj.dump(message, mode: :compat)) if authenticated?
end
subscribe(*a, &blk) click to toggle source
# File lib/slanger/channel.rb, line 58
def subscribe(*a, &blk)
  Slanger::Redis.hincrby("channel_subscriber_count", channel_id, 1).
    callback do |value|
    Slanger::Webhook.post name: "channel_occupied", channel: channel_id if value == 1
  end

  channel.subscribe *a, &blk
end
unsubscribe(*a, &blk) click to toggle source
# File lib/slanger/channel.rb, line 67
def unsubscribe(*a, &blk)
  Slanger::Redis.hincrby("channel_subscriber_count", channel_id, -1).
    callback do |value|
    Slanger::Webhook.post name: "channel_vacated", channel: channel_id if value == 0
  end

  channel.unsubscribe *a, &blk
end

Private Instance Methods

perform_client_webhook!(message) click to toggle source
# File lib/slanger/channel.rb, line 97
def perform_client_webhook!(message)
  if message["event"].start_with?("client-")
    event = message.merge({ "name" => "client_event" })
    event["data"] = Oj.dump(event["data"])

    Slanger::Webhook.post(event)
  end
end