Exploring Turbo and Hotwire, I came across a possible limitation of the broadcasting from the model approach. Maybe there is a solution, but here it goes:
Here’s the scenario. I have an boards/show.html.erb
<h1>Board <%= @board.name %> Desktop</h1>
And I have boards/show.html+phone.erb
<h1>Board <%= @board.name %> Mobile</h1>
When the board is updated, there is a broadcasts_to call.
broadcasts_to self
Simple.
If the user is looking on this page on the desktop and someone updates the board, TurboStream sent back via modal’s broadcast replaces content using the right template (show.html.erb).
If the user is looking on this page on the mobile and someone updates the board, TurboStream broadcast command has no way of knowing which template to send to the user so it sends the (show.html.erb) instead of show.html+phone.erb
If the update is triggered by an outside process, ie. background job, turbostream can send updates to connected clients, but I don’t see any option to force the correct variant.
Maybe Strada is the solution, but it’s not here yet.
One option is instead of sending back partials from models, TurboStream should send a request to self-update.
For example, if you’re looking at a page and receive a turbo_stream_refresh command, Turbo sends a fetch request to the current page in the background, gets the new html and replaces it.
Maybe TurboStream should work on this principle. Instead of pushing updates to clients it should just say “Hey, there’s an update to this page available, refresh it” or “Hey, there’s an update to a section of this page available, go and fetch it and refresh it”.