Class: ActionCable::Connection::Subscriptions

Inherits:
Object
  • Object
show all
Defined in:
lib/action_cable/connection/subscriptions.rb

Overview

Collection class for all the channel subscriptions established on a given connection. Responsible for routing incoming commands that arrive on the connection to the proper channel. Should not be used directly by the user.

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Subscriptions

Returns a new instance of Subscriptions.


6
7
8
9
# File 'lib/action_cable/connection/subscriptions.rb', line 6

def initialize(connection)
  @connection = connection
  @subscriptions = {}
end

Instance Method Details

#add(data) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/action_cable/connection/subscriptions.rb', line 23

def add(data)
  id_key = data['identifier']
  id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access

  subscription_klass = connection.server.channel_classes[id_options[:channel]]

  if subscription_klass
    subscriptions[id_key] ||= subscription_klass.new(connection, id_key, id_options)
  else
    logger.error "Subscription class not found (#{data.inspect})"
  end
end

#execute_command(data) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/action_cable/connection/subscriptions.rb', line 11

def execute_command(data)
  case data['command']
  when 'subscribe'   then add data
  when 'unsubscribe' then remove data
  when 'message'     then perform_action data
  else
    logger.error "Received unrecognized command in #{data.inspect}"
  end
rescue Exception => e
  logger.error "Could not execute command from #{data.inspect}) [#{e.class} - #{e.message}]: #{e.backtrace.first(5).join(" | ")}"
end

#identifiersObject


47
48
49
# File 'lib/action_cable/connection/subscriptions.rb', line 47

def identifiers
  subscriptions.keys
end

#perform_action(data) ⇒ Object


42
43
44
# File 'lib/action_cable/connection/subscriptions.rb', line 42

def perform_action(data)
  find(data).perform_action ActiveSupport::JSON.decode(data['data'])
end

#remove(data) ⇒ Object


36
37
38
39
40
# File 'lib/action_cable/connection/subscriptions.rb', line 36

def remove(data)
  logger.info "Unsubscribing from channel: #{data['identifier']}"
  subscriptions[data['identifier']].unsubscribe_from_channel
  subscriptions.delete(data['identifier'])
end

#unsubscribe_from_allObject


51
52
53
# File 'lib/action_cable/connection/subscriptions.rb', line 51

def unsubscribe_from_all
  subscriptions.each { |id, channel| channel.unsubscribe_from_channel }
end