Module: ActionCable::Server::Connections

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

Overview

Collection class for all the connections that's been established on this specific server. Remember, usually you'll run many cable servers, so you can't use this collection as an full list of all the connections established against your application. Use RemoteConnections for that. As such, this is primarily for internal use.

Constant Summary collapse

BEAT_INTERVAL =
3

Instance Method Summary collapse

Instance Method Details

#add_connection(connection) ⇒ Object


13
14
15
# File 'lib/action_cable/server/connections.rb', line 13

def add_connection(connection)
  connections << connection
end

#connectionsObject


9
10
11
# File 'lib/action_cable/server/connections.rb', line 9

def connections
  @connections ||= []
end

#open_connections_statisticsObject


32
33
34
# File 'lib/action_cable/server/connections.rb', line 32

def open_connections_statistics
  connections.map(&:statistics)
end

#remove_connection(connection) ⇒ Object


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

def remove_connection(connection)
  connections.delete connection
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 receive and send to it. So there's a 3 second heartbeat running on all connections. If the beat fails, we automatically disconnect.


24
25
26
27
28
29
30
# File 'lib/action_cable/server/connections.rb', line 24

def setup_heartbeat_timer
  EM.next_tick do
    @heartbeat_timer ||= EventMachine.add_periodic_timer(BEAT_INTERVAL) do
      EM.next_tick { connections.map &:beat }
    end
  end
end