Class: ActionCableClient

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

Defined Under Namespace

Classes: Commands, Message, MessageFactory

Constant Summary collapse

VERSION =
'1.2.4'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, channel = '', queued_send = false) ⇒ ActionCableClient

Returns a new instance of ActionCableClient.

Parameters:

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

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

    e.g.: RoomChannel

  • queued_send (Boolean) (defaults to: false)
    • optionally send messages after a ping

    is received, rather than instantly



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/action_cable_client.rb', line 38

def initialize(uri, channel = '', queued_send = false)
  @_channel_name = channel
  @_uri = uri
  @_queued_send = queued_send
  @message_queue = []
  @subscribed = false

  @_message_factory = MessageFactory.new(channel)
  # NOTE:
  #   EventMachine::WebSocketClient
  #      https://github.com/mwylde/em-websocket-client/blob/master/lib/em-websocket-client.rb
  #   is a subclass of
  #      https://github.com/eventmachine/eventmachine/blob/master/lib/em/connection.rb
  @_websocket_client = EventMachine::WebSocketClient.connect(_uri)
end

Instance Attribute Details

#_channel_nameObject (readonly)

Returns the value of attribute _channel_name.



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

def _channel_name
  @_channel_name
end

#_message_factoryObject (readonly)

Returns the value of attribute _message_factory.



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

def _message_factory
  @_message_factory
end

#_queued_sendObject (readonly)

Returns the value of attribute _queued_send.



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

def _queued_send
  @_queued_send
end

#_uriObject (readonly)

Returns the value of attribute _uri.



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

def _uri
  @_uri
end

#_websocket_clientObject (readonly)

Returns the value of attribute _websocket_client.



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

def _websocket_client
  @_websocket_client
end

#message_queueObject

The queue should store entries in the format:

action, data


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

def message_queue
  @message_queue
end

#subscribedObject Also known as: subscribed?

The queue should store entries in the format:

action, data


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

def subscribed
  @subscribed
end

Instance Method Details

#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


91
92
93
94
95
96
# File 'lib/action_cable_client.rb', line 91

def connected
  _websocket_client.callback do
    subscribe
    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



56
57
58
59
60
61
62
# File 'lib/action_cable_client.rb', line 56

def perform(action, data)
  if _queued_send
    message_queue.push([action, data])
  else
    dispatch_message(action, data)
  end
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



76
77
78
79
80
81
82
# File 'lib/action_cable_client.rb', line 76

def received(skip_pings = true)
  _websocket_client.stream do |message|
    handle_received_message(message, skip_pings) do |json|
      yield(json)
    end
  end
end