Skip to content

need instructions for getting action-cable working on heroku #230

@catmando

Description

@catmando

This is not really a hyperstack issue, but its going to come up and it should be documented someplace.

To get hyper-operation and hyper-model working with push-notifications on heroku with action cable you need to change some configuration from the defaults. This can all be figured out from the standard heroku documentation (https://blog.heroku.com/real_time_rails_implementing_websockets_in_rails_5_with_action_cable), but it would be helpful to summarize it in a Hyperstack Doc as well.

Before proceeding

Remember that the default Hyperstack installation turns off all broadcast policies in production. Heroku runs in production mode, so you will either have to develop a set of production security policies, or for test purposes you can let the "wide-open" policies run in production by removing the guard at the end of the app/policies/application_policy.rb file:

# app/policies/application_policy.rb
class Hyperstack::ApplicationPolicy
...
end #unless Rails.env.production? <- comment out the guard

Options for Hyperstack push notifications on Heroku

You have the following options for getting hyper-operation and hyper-model push notifications on heroku:

  1. Forget ActionCable - Just use simple polling

    This really should be the last gasp scenario. In config/initializers/hyperstack.rb set the transport config setting to :simple_poller.

  2. Use ActionCable in async mode

    This will work fine as long as you are not going to do add dynos to heroku. Basically use this if you are doing a demo app, and don't want to bother with the more complicated next step.

    Update config/cable.yml like this:

production:
  adapter: async


and finally edit config/environments/production.rb and add these two lines:

config.action_cable.allowed_request_origins = ['https://action-cable-example.herokuapp.com', 'http://action-cable-example.herokuapp.com']
config.web_socket_server_url = "wss://action-cable-example.herokuapp.com/"
  1. Use redis as the ActionCable event data base

    This requires a couple of steps but will give you a production ready solution:

Note some of these instructions need the "name" of your heroku app. It typically looks like cryptic-sierra-74917 unless you gave it an explicit name when you created the app. In the following we assume the app is named action-cable-example. Replace that with your app's name.

First Add the redis gem to your gemfile:

gem 'redis', '~> 3.0'

don't forget to bundle install!

Now we need to provision redis on your heroku instance:

Run

heroku addons:add redistogo

in your terminal window to add redis to your heroku app.

then (assuming your heroku app is named action-cable-example for example)

heroku config --app action-cable-example | grep REDISTOGO_URL

which will return a string like this:

REDISTOGO_URL:            redis://redistogo:d0ed635634356d4408c1effb00bc9493@hoki.redistogo.com:9247/

you will need the url (starting with redis:...) in the next step.

Now edit config/cable.yml so that the production entry looks like this:

production:
  adapter: redis
  url: redis://redistogo:d0ed635634356d4408c1effb00bc9493@hoki.redistogo.com:9247/

Notice the URL is the one copied from above.

Finally edit config/environments/production.rb and add these two lines:

config.action_cable.allowed_request_origins = ['https://action-cable-example.herokuapp.com', 'http://action-cable-example.herokuapp.com']
config.web_socket_server_url = "wss://action-cable-example.herokuapp.com/"

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs docEverything is working, but documentation is needed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions