Class: ActionCable::Connection::Subscriptions
- Defined in:
- actioncable/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.
Instance Method Summary collapse
- #add(data) ⇒ Object
- #execute_command(data) ⇒ Object
- #identifiers ⇒ Object
-
#initialize(connection) ⇒ Subscriptions
constructor
A new instance of Subscriptions.
- #perform_action(data) ⇒ Object
- #remove(data) ⇒ Object
- #remove_subscription(subscription) ⇒ Object
- #unsubscribe_from_all ⇒ Object
Constructor Details
#initialize(connection) ⇒ Subscriptions
Returns a new instance of Subscriptions.
10 11 12 13 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 10 def initialize(connection) @connection = connection @subscriptions = {} end |
Instance Method Details
#add(data) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 27 def add(data) id_key = data["identifier"] = ActiveSupport::JSON.decode(id_key).with_indifferent_access return if subscriptions.key?(id_key) subscription_klass = [:channel].safe_constantize if subscription_klass && ActionCable::Channel::Base >= subscription_klass subscription = subscription_klass.new(connection, id_key, ) subscriptions[id_key] = subscription subscription.subscribe_to_channel else logger.error "Subscription class not found: #{[:channel].inspect}" end end |
#execute_command(data) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 15 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.}]: #{e.backtrace.first(5).join(" | ")}" end |
#identifiers ⇒ Object
58 59 60 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 58 def identifiers subscriptions.keys end |
#perform_action(data) ⇒ Object
54 55 56 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 54 def perform_action(data) find(data).perform_action ActiveSupport::JSON.decode(data["data"]) end |
#remove(data) ⇒ Object
44 45 46 47 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 44 def remove(data) logger.info "Unsubscribing from channel: #{data['identifier']}" remove_subscription find(data) end |
#remove_subscription(subscription) ⇒ Object
49 50 51 52 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 49 def remove_subscription(subscription) subscription.unsubscribe_from_channel subscriptions.delete(subscription.identifier) end |
#unsubscribe_from_all ⇒ Object
62 63 64 |
# File 'actioncable/lib/action_cable/connection/subscriptions.rb', line 62 def unsubscribe_from_all subscriptions.each { |id, channel| remove_subscription(channel) } end |