stream_from
is great, there are many techniques only possible if you leverage ActionCable Connections, Channels and Subscriptions.stream_from
helper, which was covered in the previous Hello World chapter.routes.rb
maps request paths to Controllers. The way you structure these identifiers decides whether messages sent to the Channel are delivered to every subscriber, or just a subset that matches a given pattern.consumer
to subscribe to Channels. The subscription provides access to data that is received
from the server, as well as a mechanism for sending data to the sever.params
, similar to POSTing a form to a controller action. The Channel class can access these params
and use these values to compute and return a subscription identifier. 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.ExampleChannel
will receive any broadcasts sent to to the identifier visitors
. Operations broadcast there will be sent to everyone looking at your site. They will be automatically subscribed to your channel.CableReady
and modify the received
method to check incoming data for CableReady broadcasts.ExampleChannel
that you created will send any operations broadcast to visitors
to all currently subscribed clients. In the code above, everyone on the site will see a Console Inspector message welcoming the latest member.ExampleChannel
from visitors
because only one Channel can stream from a given identifier. It is conceptually similar to Rails request routing, except that identifiers are defined inside of your Channel classes.cable_ready
multiple times to add more operations to these queues.cable_ready
is a singleton instance, which means that you can keep adding operations to a queue across multiple method calls.selector
, which is how you identify the target DOM element(s) for an operation. In fact, it's so common that you can just pass it as the first parameter, without a key.broadcast
.cable_ready["visitors"]
, it returns a CableReady::Channels
object, which supports method chaining. That is, you can link up as many operations in sequence as you want, and they will be broadcast in the order that they were created.broadcast
method concludes the chain. After the operations have been dispatched, the queues are emptied.