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.0'

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



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

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

#_message_factoryObject (readonly)

Returns the value of attribute _message_factory.



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

def _message_factory
  @_message_factory
end

#_subscribedObject

The queue should store entries in the format:

action, data


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

def _subscribed
  @_subscribed
end

#_subscribed_callabackObject

The queue should store entries in the format:

action, data


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

def _subscribed_callaback
  @_subscribed_callaback
end

#_uriObject (readonly)

Returns the value of attribute _uri.



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

def _uri
  @_uri
end

#_websocket_clientObject (readonly)

Returns the value of attribute _websocket_client.



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

def _websocket_client
  @_websocket_client
end

#message_queueObject

The queue should store entries in the format:

action, data


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

def message_queue
  @message_queue
end

Instance Method Details

#connect!(headers = {}) ⇒ Object



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

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


97
98
99
100
101
102
# File 'lib/action_cable_client.rb', line 97

def connected
  _websocket_client.onopen 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


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

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



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

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

#received(skip_pings = true) ⇒ Object

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

Parameters:

  • skip_pings (Boolean) (defaults to: true)
    • by default, messages

    with the identifier ‘_ping’ are skipped



82
83
84
85
86
87
88
# File 'lib/action_cable_client.rb', line 82

def received(skip_pings = true)
  _websocket_client.onmessage do |message, _type|
    handle_received_message(message, skip_pings) 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



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

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

#subscribed?Boolean

Returns is the client subscribed to the channel?.

Returns:

  • (Boolean)

    is the client subscribed to the channel?



122
123
124
# File 'lib/action_cable_client.rb', line 122

def subscribed?
  _subscribed
end