Using AnyCable with Ruby (non Rails)

Vladimir Dementyev edited this page Oct 21, 2017 · 4 revisions

AnyCable can be used without Rails, thus allowing you to use ActionCable-like functionality in your app.

Lite Cable

There is a ready-to-go framework – Lite Cable – which can be used for application logic. It also supports AnyCable out-of-the-box.

See Lite Cable Sinatra example and read about Connecting LiteCable to Hanami by @GabrielMalakias.

Custom Backend Framework

You can build your own framework to use as logic-handler for AnyCable.

AnyCable initiates a connection object for every request using user-provided factory:

# Specify factory
AnyCable.connection_factory = MyConnectionFactory

# And then AnyCable calls .call method on your factory
connection =, options)


  • socket – is an object, representing client's socket (say, socket stub) (see socket.rb)

  • options may contain:

    • identifiers – a JSON string returned by connection.identifiers_json on connection (see below)
    • subscriptions – a list of channels identifiers for the connection.

Connection interface:

class Connection
  # Called on connection
  def handle_open
  # Called on disconnection
  def handle_close
  # Called on incoming message.
  # Client send a JSON-encoded message of the form { "identifier": ..., "command": ..., "data" ... }.
  # - identifier – channel identifier (e.g. `{"channel":"chat","id":1}`)
  # - command – e.g. "subscribe", "unsubscribe", "message"
  # - any additional data
  def handle_channel_command(identifier, command, data)
  # Returns any string which can be used later in .create function to initiate connection.
  def identifiers_json

Connection#handle_channel_command should return truthy value on success (i.e. when subscription is confirmed or action is called).

NOTE: connection instance is initiated on every request, so it should be state-less (except from identifiers_json).

To send a message to a client you should call socket#transmit. For manipulating with streams use socket#subscribe, socket#unsubscribe and socket#unsubscribe_from_all.

See test factory for example.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.