Class: ActionCableClient

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/action_cable_client.rb,
lib/action_cable_client/errors.rb,
lib/action_cable_client/message.rb,
lib/action_cable_client/version.rb,
lib/action_cable_client/message_factory.rb

Defined Under Namespace

Modules: Errors Classes: Commands, Message, MessageFactory

Constant Summary collapse

VERSION =
'2.0.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, params = '', connect_on_start = true, headers = {}) ⇒ ActionCableClient

Returns a new instance of ActionCableClient.

Parameters:

  • uri (String)
    • e.g.: ws://domain:port

  • params (String) (defaults to: '')
    • the name of the channel on the Rails server

    or params. This gets sent with every request.

    e.g.: RoomChannel
    
  • connect_on_start (Boolean) (defaults to: true)
    • connects on init when true

    • otherwise manually call ‘connect!`

  • headers (Hash) (defaults to: {})
    • HTTP headers to use in the handshake



37
38
39
40
41
42
43
44
45
# File 'lib/action_cable_client.rb', line 37

def initialize(uri, params = '', connect_on_start = true, headers = {})
  @_uri = uri
  @message_queue = []
  @_subscribed = false

  @_message_factory = MessageFactory.new(params)

  connect!(headers) if connect_on_start
end

Instance Attribute Details

#_connected_callbackObject

The queue should store entries in the format:

action, data


25
26
27
# File 'lib/action_cable_client.rb', line 25

def _connected_callback
  @_connected_callback
end

#_message_factoryObject (readonly)

Returns the value of attribute _message_factory.



22
23
24
# File 'lib/action_cable_client.rb', line 22

def _message_factory
  @_message_factory
end

#_pinged_callbackObject

The queue should store entries in the format:

action, data


25
26
27
# File 'lib/action_cable_client.rb', line 25

def _pinged_callback
  @_pinged_callback
end

#_subscribedObject

The queue should store entries in the format:

action, data


25
26
27
# File 'lib/action_cable_client.rb', line 25

def _subscribed
  @_subscribed
end

#_subscribed_callbackObject

The queue should store entries in the format:

action, data


25
26
27
# File 'lib/action_cable_client.rb', line 25

def _subscribed_callback
  @_subscribed_callback
end

#_uriObject (readonly)

Returns the value of attribute _uri.



21
22
23
# File 'lib/action_cable_client.rb', line 21

def _uri
  @_uri
end

#_websocket_clientObject (readonly)

Returns the value of attribute _websocket_client.



21
22
23
# File 'lib/action_cable_client.rb', line 21

def _websocket_client
  @_websocket_client
end

#message_queueObject

The queue should store entries in the format:

action, data


25
26
27
# File 'lib/action_cable_client.rb', line 25

def message_queue
  @message_queue
end

Instance Method Details

#connect!(headers = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/action_cable_client.rb', line 47

def connect!(headers = {})
  # Quick Reference for WebSocket::EM::Client's api
  # - onopen - called after successfully connecting
  # - onclose - called after closing connection
  # - onmessage - called when client recives a message. on `message do |msg, type (text or binary)|``
  #             - also called when a ping is received
  # - onerror - called when client encounters an error
  # - onping - called when client receives a ping from the server
  # - onpong - called when client receives a pong from the server
  # - send - sends a message to the server (and also disables any metaprogramming shenanigans :-/)
  # - close - closes the connection and optionally sends close frame to server. `close(code, data)`
  # - ping - sends a ping
  # - pong - sends a pong
  @_websocket_client = WebSocket::EventMachine::Client.connect(uri: @_uri, headers: headers)
end

#connectedObject

callback when the client connects to the server

Examples:

client = ActionCableClient.new(uri, 'RoomChannel')
client.connected do
  # do things after the client is connected to the server
end


93
94
95
96
97
98
# File 'lib/action_cable_client.rb', line 93

def connected
  self._connected_callback = Proc.new do
    subscribe
    yield
  end
end

#disconnectedObject

callback when the server disconnects from the client.

Examples:

client = ActionCableClient.new(uri, 'RoomChannel')
client.connected {}
client.disconnected do
  # cleanup after the server disconnects from the client
end


130
131
132
133
134
135
# File 'lib/action_cable_client.rb', line 130

def disconnected
  _websocket_client.onclose do
    self._subscribed = false
    yield
  end
end

#perform(action, data) ⇒ Object

Parameters:

  • action (String)
    • how the message is being sent

  • data (Hash)
    • the message to be sent to the channel



65
66
67
# File 'lib/action_cable_client.rb', line 65

def perform(action, data)
  dispatch_message(action, data)
end

#pinged(&block) ⇒ Object



137
138
139
# File 'lib/action_cable_client.rb', line 137

def pinged(&block)
  self._pinged_callback = block
end

#receivedObject

callback for received messages as well as what triggers depleting the message queue

Examples:

client = ActionCableClient.new(uri, 'RoomChannel')
client.received do |message|
  # the received message will be JSON
  puts message
end


78
79
80
81
82
83
84
# File 'lib/action_cable_client.rb', line 78

def received
  _websocket_client.onmessage do |message, _type|
    handle_received_message(message) do |json|
      yield(json)
    end
  end
end

#subscribed(&block) ⇒ Object

callback when the client receives a confirm_subscription message from the action_cable server. This is only called once, and signifies that you can now send messages on the channel

Examples:

client = ActionCableClient.new(uri, 'RoomChannel')
client.connected {}
client.subscribed do
  # do things after successful subscription confirmation
end

Parameters:

  • block (Proc)
    • code to run after subscribing to the channel is confirmed



113
114
115
# File 'lib/action_cable_client.rb', line 113

def subscribed(&block)
  self._subscribed_callback = block
end

#subscribed?Boolean

Returns is the client subscribed to the channel?.

Returns:

  • (Boolean)

    is the client subscribed to the channel?



118
119
120
# File 'lib/action_cable_client.rb', line 118

def subscribed?
  _subscribed
end