bundle add cable_readyyarn add cable_ready
bundle exec rails generate channel example
app/channels/example_channel.rbclass ExampleChannel < ApplicationCable::Channeldef subscribedstream_from "example-stream"endend
app/views/home/index.html.erb<h1>What will happen?</h1><div id="content"></div>
You can call ActionCable from an ActiveJob, an ActiveRecord callback, a rake task, inside of a StimulusReflex action method. Here we'll launch an ActiveJob from our controller. Five seconds after the page loads, you will see an update.
app/controllers/home_controller.rbclass HomeController < ApplicationControllerdef indexExampleJob.set(wait: 5.seconds).perform_laterendend
app/jobs/example_job.rbclass ExampleJob < ApplicationJobinclude CableReady::Broadcasterqueue_as :defaultdef perform(*args)cable_ready["example-stream"].inner_html(selector: "#content",html: "Hello World this is the background job.")cable_ready.broadcastendend
CableReady supports quite a few DOM operations that can be broadcast to connected clients. View the full list here.