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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ActionCableClient.

Parameters:

    • e.g.: ws://domain:port

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

    e.g.: RoomChannel

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

    is received, rather than instantly



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/action_cable_client.rb', line 35

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

#_subscribedObject

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

#_subscribed_callabackObject

The queue should store entries in the format:

action, data


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

def _subscribed_callaback
  @_subscribed_callaback
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

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


88
89
90
91
92
93
# File 'lib/action_cable_client.rb', line 88

def connected
  _websocket_client.callback do
    subscribe
    yield
  end
end

#perform(action, data) ⇒ Object

Parameters:

    • how the message is being sent

    • the message to be sent to the channel



53
54
55
56
57
58
59
# File 'lib/action_cable_client.rb', line 53

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:

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

    with the identifier ‘_ping’ are skipped



73
74
75
76
77
78
79
# File 'lib/action_cable_client.rb', line 73

def received(skip_pings = true)
  _websocket_client.stream do |message|
    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:

    • code to run after subscribing to the channel is confirmed



108
109
110
# File 'lib/action_cable_client.rb', line 108

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

#subscribed?Boolean

Returns is the client subscribed to the channel?.

Returns:

  • is the client subscribed to the channel?



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

def subscribed?
  _subscribed
end