Client-Side Action Cable connection with Rails 5 Server

Here is a gem that will allow you a slim interface for connecting to Rails 5’s Action Cable: GitHub - NullVoxPopuli/action_cable_client: A ruby client for interacting with Rails' ActionCable. -- Maintainers Wanted.

A simple example:

require 'action_cable_client'

EventMachine.run do

  uri = "ws://localhost:3000/cable/"
  client = ActionCableClient.new(uri, 'RoomChannel')
  # the connected callback is required, as it triggers
  # the actual subscribing to the channel but it can just be
  # client.connected {}
  client.connected { puts 'successfully connected.' }

  # called whenever a message is received from the server
  client.received do | message |
    puts message
  end

  # adds to a queue that is purged upon receiving of
  # a ping from the server
  client.perform('speak', { message: 'hello from amc' })
end

calling perform puts messages in to a queue, and they are all flushed when the next ping from the server is received.

1 Like