1

I'm playing around with the new ActionCable feature. Is there a way of communicating with the ActionCable server using say socket.io or from an application using React or ReactNative?

Perhaps I'm confusing the correct use of ActionCable and it is not intended to be used as a let's say API replacement and it is meant to be used as a supporting front end technology for the same app.

Any good example or guide to use ActionCable as standalone WS server would be appreciated if this is possible.

Saul Martínez
  • 920
  • 13
  • 28

1 Answers1

1

You can interact with ActionCable as you would normally with any WebSocket libraries.

To do so, you would stream from a Channel in Rails:

class ExampleChannel < ApplicationCable::Channel
  def subscribe
    stream_from 'example'
  end
end

Then, you may connect to the Rails WebSocket through your stand-alone client and subscribe to the message using the ActionCable protocol:

function Socket(url) {
  const ws = new WebSocket(url);

  ws.onopen(() => {
    ws.send('{"command":"subscribe","identifier":"{\"channel\":\"ExampleChannel\"}"');
  });
}

Reference: http://edgeguides.rubyonrails.org/action_cable_overview.html#channels

https://github.com/NullVoxPopuli/action_cable_client/blob/master/README.md

  • Yeah, I was reading how to implement the subscription messages and such, but [ActionCable NPM package came out](https://www.npmjs.com/package/actioncable) – Saul Martínez Sep 20 '16 at 23:11
  • @SaulMartínez have you been able to communicate with actioncable server from iOS using actioncable npm package. If so can you share sample code? – Zahid May 12 '17 at 14:44