4

I need to use Websocket in my Rails API (only) app. There's no any server-side rendering for pages. The app is a JSON-only backend for Angular and Xamarin. I saw that a lot of tutorials use coffescript as ActionCable client inside Rails, but it's not my case.

The question is: how to use ActionCable as API-only, outside of Rails' coffescript, like for Android, iOS, Angular and React client?

I found different and useful resources but I'm still a bit confused.

1: ActionCable usage for Rails API - it seems the best, but how can I find all JSON commands exchanged by the ActionCable framework?

2: Similar Question to the mine

3: Rails API Websocket GEM - ok, is not ActionCable, but this guy had compiled coffescript into JS and ported it into project. Not useful for Mobile development.

If there are different solutions or better knowledges, please tell me. I need to "hack" this to turn it working.

EDIT : I found also this repo action-cable-js but how to integrate it?

Marco Sanfilippo
  • 293
  • 3
  • 19
  • Will using Websockets without ActionCable be an option? if so, consider the [`iodine` gem](https://github.com/boazsegev/iodine). It should also improve performance significantly (when compared to ActionCable). – Myst Sep 09 '17 at 15:43
  • @Myst that you for your reply. The strategy is not "so easy" because the backend is made up of N containers for Rails, load balanced by Nginx. So, there's one (or maybe M, load balanced) separate ActionCable servers, run as stand alone server. In thet case, I think __iodine__ or ActionCable is a matter of performance, but I need to redesign the whole application. I will check the gem deeply. Thank you! – Marco Sanfilippo Sep 11 '17 at 07:41

2 Answers2

3

Actioncable for API only has got official support and there's this npm package called actioncable which has decoupled support for communication with actioncable server using an API only approach.

npm install actioncable

then in your react/angular code

import ActionCable from 'actioncable';

# create a connection with the actioncable server
# use ws:// for http and wss:// for https
const cable = ActionCable.createConsumer("ws://your-backend-server.com/cable");
# now you create a subscription
my_subscription = cable.subscriptions.create("MessengerChannel", {
  connected: function() {
    console.log("You've subscribed to the Messenger Channel");
  },
  disconnected: function() {
    console.log("You've disconnected from the Messenger Channel");
  },
  received: function (received_data) {
    # broadcast data from the Messenger channel is received here
    console.log(received_data);
  }
})

At the back-end side, you need to mount actioncable, for that do this

# routes.rb file
Rails.application.routes.draw do
  mount ActionCable.server => '/cable'
end
Masroor
  • 1,484
  • 3
  • 14
  • 23
2

I hope my answer could be useful for anybody. I finally discovered a library called actioncable-js that helped me in the Angular project.

But is not enough. For the Mobile projects I need to use "low level" commands to interact with Action Cable server.

The protocol is here: protocol.

But if you need more about you should look deep inside the gem.

Marco Sanfilippo
  • 293
  • 3
  • 19