23

I built a very simple app using Rails 5 beta 1 and ActionCable to show when users come online and let them send messages to each other.

Now, I would basically like to take the client-side part of ActionCable, implement it in the context of another app (that does not run on Rails 5) and connect it with the first app to send and receive data (such as the online status of users or messages).

To send data from that second app, I assume, I can simply make an AJAX POST request. The question is: How do I subscribe from my second app to an open connection of the first app?

Or even: How do I subscribe to the ActionCable connection of my Rails app from another app via API?

My guess is, I essentially want to include this coffeescript somehow in my second app:

App.appearance = App.cable.subscriptions.create "AppearanceChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    # ...
sam
  • 1,711
  • 1
  • 17
  • 24

2 Answers2

23

You'll essentially need to include a copy or port of the ActionCable JS code in your other app (https://github.com/rails/rails/tree/master/actioncable/app/assets/javascripts).

Update: I recently released an npm package called actioncable-js that provides a direct port of Ruby on Rails 5's ActionCable CofeeScript to standard JS for use outside of Rails: https://github.com/mwalsher/actioncable-js

Because ActionCable is just a layer on top of HTML5 WebSockets, so you can also use raw WebSockets JS code (or any third-party library) to handle the messaging.

The ActionCable message protocol is somewhat documented here: https://github.com/NullVoxPopuli/action_cable_client#the-action-cable-protocol. I'll paste below for convenience:

  1. Connect to the Action Cable URL
  2. After the connection succeeds, send a subscribe message

    • The subscribe message JSON should look like this: {"command":"subscribe","identifier":"{\"channel\":\"MeshRelayChannel\"}"}
    • You should receive a message like this: {"identifier"=>"{\"channel\":\"MeshRelayChannel\"}", "type"=>"confirm_subscription"}
  3. Once subscribed, you can send messages.

    • Make sure that the action string matches the data-handling method name on your ActionCable server.
    • Your message JSON should look like this: {"command":"message","identifier":"{\"channel\":\"MeshRelayChannel\"}","data":"{\"to\":\"user1\",\"message\":\"hello from user2\",\"action\":\"chat\"}"}
    • Received messages should look about the same
  4. Notes:

    • Every message sent to the server has a command and identifier key.
    • The channel value must match the name of the channel class on the ActionCable server.
    • identifier and data are redundantly jsonified.

So, for example (in ruby)

payload = {
  command: 'command text',
  identifier: { channel: 'MeshRelayChannel' }.to_json,
  data: { to: 'user', message: 'hi', action: 'chat' }.to_json
}.to_json
mwalsher
  • 2,790
  • 2
  • 33
  • 41
  • Yes. I was looking into that as well. I feel like it's not as straight forward, though. At least not for me. There are some parts that need to be adjusted and can't simply be copied. Unfortunately, the ES6 port appears not to work with the most recent version of Rails 5. – sam Mar 22 '16 at 09:16
  • Updated my answer with a link to a direct port of the ActionCable JS: https://github.com/mwalsher/actioncable-js. Hopefully this helps! – mwalsher Apr 26 '16 at 21:23
  • Really, really nice solution. Thanks for putting this together! This is extremely helpful. I decided to post an answer on our own because I just found a pull request on the Rails repo with an official NPM package. – sam Jun 28 '16 at 07:29
  • For example, I want to socket.io-client for that. How do I connect to `MeshRelayChannel ` using `socket.io-client`? – sreang rathanak Dec 20 '17 at 14:23
23

While mwalsher's solution was extremely helpful to me, I recently found a pull request on the Rails repository with an official solution to my question.

https://github.com/rails/rails/pull/24991

I assume, in the near future this will be added to the main documentation. Here is the link to the official actioncable npm package: https://www.npmjs.com/package/actioncable

You can use it similar to mwalsher's solution with any JS app. Just install the npm package:

npm install actioncable --save

Here the JS example from the documentation:

ActionCable = require('actioncable')

var cable = ActionCable.createConsumer('wss://RAILS-API-PATH.com/cable')

cable.subscriptions.create('AppearanceChannel', {
  // normal channel code goes here...
});

Edit: The pull request has been merged for a while, now and the description is part of the official Readme - just not yet in the Rails Guides.

sam
  • 1,711
  • 1
  • 17
  • 24
  • @jim broski please share the open Repository of the two applications which will useful to developers new to rails and actioncable. At least share a post on it. It will be useful. – Modi Ranga Nayakulu Oct 04 '16 at 06:16