This PR updates the BroadcastableMixin and TurboStreamsConsumer to bundle all updates for a given model into a single stream rather than require separate stream subscriptions to accomplish this.
This brings our BroadcastableMixin in line with the Broadcastable found in turbo-rails. A few options now can be set on a model which inherits from BroadcastableMixin:
broadcast_to: List of foreign key fields whose streams should be broadcast to when this field updates.
For example, in the chat demo, the Message model has broadcast_to = ["room"], so when a message is created or updated, any clients subscribed to its room's stream receive updates.
broadcast_self: Whether to broadcast updates on the model's own stream. Defaults to True. Message sets this to False since there's never an instance where we subscribe to updates for a single message.
inserts_by: This can either be append or prepend. Defaults to append. Determines whether new instances will be append or prepended to a given list of instances.
One important TODO for another PR is allowing a custom template for a model. Right now it defaults to using app/model.html which is fine for demos but real applications should be able to customize this. Potentially even have this be a per-subscription option.
The chat demo has also been updated to include editing the room title in-line, just like the Hotwire video demo. Any feedback on either the code or the updated demo is appreciated!
This PR updates the
BroadcastableMixin
andTurboStreamsConsumer
to bundle all updates for a given model into a single stream rather than require separate stream subscriptions to accomplish this.This brings our
BroadcastableMixin
in line with theBroadcastable
found inturbo-rails
. A few options now can be set on a model which inherits fromBroadcastableMixin
:broadcast_to
: List of foreign key fields whose streams should be broadcast to when this field updates.Message
model hasbroadcast_to = ["room"]
, so when a message is created or updated, any clients subscribed to its room's stream receive updates.broadcast_self
: Whether to broadcast updates on the model's own stream. Defaults toTrue
.Message
sets this toFalse
since there's never an instance where we subscribe to updates for a single message.inserts_by
: This can either beappend
orprepend
. Defaults toappend
. Determines whether new instances will be append or prepended to a given list of instances.One important TODO for another PR is allowing a custom template for a model. Right now it defaults to using
app/model.html
which is fine for demos but real applications should be able to customize this. Potentially even have this be a per-subscription option.The
chat
demo has also been updated to include editing the room title in-line, just like the Hotwire video demo. Any feedback on either the code or the updated demo is appreciated!