Lightning Talk given at the Tokyo Rails meetup #32 about using ActionCable with React, either with the react_on_rails gem or by having a separate rails API and webpack managed frontend.
conventions set by React on Rails 2. Expose the components that you need in Rails import Hello from '../components/Hello'; import ReactOnRails from 'react-on-rails'; ReactOnRails.register({ Hello }); 3. Use the exposed components alongside AC channels in your wrapper Rails views <%= content_for :javascript do %> <%= javascript_include_tag "channels/hello" %> <% end -%> <%= react_component("Hello", props: { name: "Julien" }) %>
componentDidMount() { MYAPP.Hello = this; } updateName(data) { this.setState({ name: data.name }); } 6. On the server side Mount the ActionCable engine // config/routes.rb mount ActionCable.server => "/cable" Send messages ActionCable.server.broadcast("hello", serialized_name)
You can inspect the WS frames in the Chrome inspector from inside the initial connect request. You don't need to use Redis as a backend, PostgreSQL works just ne: development: adapter: "postgresql" url: "postgres://root:@localhost/myapp_development" ↑ not a dumb DB adapter, it uses PG's LISTEN / NOTIFY