Skip to content

Instantly share code, notes, and snippets.

@alexgriff
Last active January 30, 2019 02:50
Show Gist options
  • Save alexgriff/f34629489a09e547240b5e6cd3e3f1f5 to your computer and use it in GitHub Desktop.
Save alexgriff/f34629489a09e547240b5e6cd3e3f1f5 to your computer and use it in GitHub Desktop.
ActionCable with React

ActionCable

Lecture Videos and Code

Resources

Rishi's Notes

  • To broadcast to all subscribers, you can run ActionCable.server.broadcast('feed_name', object) in your controller action
  • The npm library to make this work in react is react-actioncable-provider
  • Wrap your app in <ActionCableProvider url={'ws://<rails_url>/cable'}></ActionCableProvider>
  • Add <ActionCable channel={ { channel: 'FeedChannel' } } onReceived={ callback } />
  • Remember, once your UI is updating on messages from the socket, you no longer need to add those elements manually. Let the ActionCable component update the state, and make sure rails publishes to the socket when that change happens on the backend
  • If you want to open different streams/sockets from the same rails app, you can stream_for [object], and you'll have to use broadcast_to in the controller. This means when you open the channel in react, your ActionCable component takes an extra prop, [object]_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment