Skip to content

Instantly share code, notes, and snippets.

@stenagli
stenagli / actioncable-react.md
Last active April 14, 2020 16:06
ActionCable on React/Heroku

ActionCable with React

This is mainly an annotation of the Rails ActionCable documentation, with additional information for using it with React and publishing to Heroku.

Setup

Most of the boilerplate files can be generated with e.g. rails g channel Game to create a channel named GameChannel.

The generator will create a subscription file in e.g. app/assets/javascripts/subscriptions/game.coffee that contains the code to create a new subscription. You can safely delete this file, because this code will be moved into a React component instead.

Signed Cookie

Section 3.1.1 discusses a signed cookie to authorize the connection. This can be accomplished by using a Warden initialization hook. Keep in mind that the initializer in the article should be placed in config/initializers/warden_hooks.rb instead of app/config/initializers/warden_hooks.rb. If your Rails server is curr