CableReady
Search…
Setup
Bootstrap a minimum viable CableReady install without bikeshedding
If your goals can be achieved by broadcasting operations to the current user from inside of a Reflex action method, you're already good to go and can skip to CableReady 101.
To demonstrate a basic setup, we're going to use the built-in Rails channel generator to create an ActionCable Channel class called ExampleChannel. If this is the first time you've generated a Channel, a number of important files and folders will be created.
1
bundle exec rails generate channel example
Copied!
In this configuration, every client that subscribes to ExampleChannel will receive any broadcasts sent to to a stream called visitors. We'll talk more about streams soon. For now, visitors is for operations that will be sent to everyone currently looking at your site.
app/channels/example_channel.rb
1
class ExampleChannel < ApplicationCable::Channel
2
def subscribed
3
stream_from "visitors"
4
end
5
end
Copied!
The generator also creates a JavaScript channel subscriber. Import CableReady and modify the received method to check incoming data for CableReady broadcasts.
app/javascript/channels/example_channel.js
1
import CableReady from 'cable_ready'
2
import consumer from './consumer'
3
4
consumer.subscriptions.create('ExampleChannel', {
5
received (data) {
6
if (data.cableReady) CableReady.perform(data.operations)
7
}
8
})
Copied!
That's it! Let's get this party started and learn how to broadcast operations.
Last modified 9mo ago
Export as PDF
Copy link