Module: ActionCable::Server::Connections

Included in:
Base
Defined in:
lib/action_cable/server/connections.rb

Overview

# Action Cable Server Connections

Collection class for all the connections that have been established on this specific server. Remember, usually you’ll run many Action Cable servers, so you can’t use this collection as a full list of all of the connections established against your application. Instead, use RemoteConnections for that.

Constant Summary collapse

BEAT_INTERVAL =

:nodoc:

3

Instance Method Summary collapse

Instance Method Details

#add_connection(connection) ⇒ Object



22
23
24
# File 'lib/action_cable/server/connections.rb', line 22

def add_connection(connection)
  connections_map[connection.object_id] = connection
end

#connectionsObject



20
# File 'lib/action_cable/server/connections.rb', line 20

def connections = connections_map.values

#connections_mapObject



16
17
18
# File 'lib/action_cable/server/connections.rb', line 16

def connections_map
  @connections_map ||= {}
end

#open_connections_statisticsObject



41
42
43
# File 'lib/action_cable/server/connections.rb', line 41

def open_connections_statistics
  connections_map.each_value.map(&:statistics)
end

#remove_connection(connection) ⇒ Object



26
27
28
# File 'lib/action_cable/server/connections.rb', line 26

def remove_connection(connection)
  connections_map.delete connection.object_id
end

#setup_heartbeat_timerObject

WebSocket connection implementations differ on when they’ll mark a connection as stale. We basically never want a connection to go stale, as you then can’t rely on being able to communicate with the connection. To solve this, a 3 second heartbeat runs on all connections. If the beat fails, we automatically disconnect.



35
36
37
38
39
# File 'lib/action_cable/server/connections.rb', line 35

def setup_heartbeat_timer
  @heartbeat_timer ||= executor.timer(BEAT_INTERVAL) do
    executor.post { connections_map.each_value(&:beat) }
  end
end