CableReady.perform()
inside an operation.cable_car
CableReady::Broadcaster
Concern now provides a cable_car
method (in addition to the familiar cable_ready
method) which you can call anywhere in your application.cable_car
to chain together operations which will ultimately be converted into JSON that the CableReady client can process.Channels
- which are delivered over WebSockets - Cable Car isn't opinionated about how you plan to use the JSON it creates. It doesn't have any broadcast capacity of its own, making it a perfect fit for controller actions responding to HTTP requests, ActiveJob scheduling and even persisting operations to your database.cable_car
is very similar to using the cable_ready
method, except that there is no stream identifier ("no square brackets"). Instead of sending data with broadcast
, you generate JSON with dispatch
:operations
is an Array that describes a batch of queued operations.operation
which identifies the operation type. Depending on the operation, additional to_json
method and send it to the client via the mechanism of your choice:CableReady.perform()
and the operations will be executed, regardless of how they got to the the browser.cable_car
and add operations multiple times, and it will continue to accumulate operations until you do something to clear the queue. Like the broadcast
method, dispatch
accepts an optional boolean keyword argument clear
, which you can use to return the current JSON blob without clearing the queue.fetch
is a snap. We need a button to kick things off, and an empty DIV element named users
to receive updates. The button calls go
on a Stimulus controller:CableReady.perform
.dispatch
on a cable_car
method chain produces a Hash that represents all of your queued operations. What if you are not quite ready to send those updates, or want to save them in your database?