For a household chores list edited by several people at once, when would you choose morphing refresh broadcasts over targeted Turbo Stream broadcasts, and why?
answer
- one signal vs one partial per change
- each viewer renders the page
- per-user markup survives a refresh
- no drift between partial and page
- cost grows with open tabs
basics
~20 sChoose refresh broadcasts when the page is cheap to render and correctness matters more than bandwidth: every viewer re-renders the real page with their own session, so nothing drifts. Choose targeted streams for heavy pages, many viewers or high-frequency updates.
solid answer
~50 sTargeted broadcasts such as `broadcasts_to` render a partial once per change and push that HTML, so each kind of change needs its own stream action and the page can drift: a counter or an empty-state message outside the partial stays stale unless another stream updates it. They also render outside any request (through `ApplicationController.render`), so per-user markup such as "assigned to you" cannot depend on the viewer. Refresh broadcasts send one signal; each tab re-fetches the page with its own cookies and morphs, so the result is exactly what a reload would show, per user, with no extra code per change. The price is a full page render per open tab per change, softened by debouncing. For a household chores list, a handful of viewers and a cheap page, refreshes are the simpler and safer choice; a dashboard watched by hundreds, or a feed updating every second, favours targeted streams.
code
ruby · 11 lines# Targeted: one partial per change, rendered without a current user
class Chore < ApplicationRecord
belongs_to :household
broadcasts_to :household, inserts_by: :prepend
end
# Refresh: one signal per change, each viewer re-renders the page
class Chore < ApplicationRecord
belongs_to :household
broadcasts_refreshes_to :household
endgo deeper
Know that targeted streams send HTML for a change, while a refresh broadcast tells each page to reload itself and morph.
Compare who renders what: one partial for everyone versus one page per viewer, and why partials outside the target go stale.
Choose per screen from viewer count, change rate, page render cost and per-user markup, and mix refreshes with targeted streams or frames where it pays.
Set a team default for live updates that keeps rendering logic in one place, and revisit it only for screens whose fan-out makes refreshes too costly.
## Two ways to keep a page live Both approaches start the same way, with a model callback after commit and a page subscribed with `turbo_stream_from`. They differ in what travels over the wire and who renders it. **Targeted streams** (`broadcasts_to`, `broadcast_replace_later_to` and friends) render HTML on the server and send it inside a `<turbo-stream>` action such as `append`, `replace` or `remove`. The page applies that fragment directly. **Refresh broadcasts** (`broadcasts_refreshes`, `broadcasts_refreshes_to`) send `<turbo-stream action="refresh">` with no HTML. Each tab re-fetches its current URL and, with `turbo_refreshes_with method: :morph`, morphs the DOM to match. ## Comparing them | Concern | Targeted streams | Refresh broadcasts | |---|---|---| | Server rendering per change | one partial, rendered once for all viewers | one full page per subscribed tab | | Code per kind of change | a stream action and target for each | none beyond the macro | | Parts of the page outside the partial (counts, empty states, badges) | stale unless more streams update them | correct, the whole page is re-rendered | | Per-viewer markup | rendered outside a request, no current user | rendered in each viewer's own request | | Latency | the fragment arrives with the broadcast | signal, then a page request, then a morph | | Client-side state | untouched outside the target | may be reset by the morph unless protected | ## Why per-user markup decides many cases turbo-rails renders broadcast partials through `ApplicationController.render`, outside any HTTP request. There is no session and no signed-in user, so `Current.user` or a `current_user` helper is not available to the partial. A chore row that shows "assigned to you" or hides an Edit button from members without permission cannot be rendered correctly once for everyone. Teams work around it with neutral partials plus Stimulus controllers that adjust the markup per viewer. A refresh avoids the problem: each tab requests the page with its own cookies, so the server renders it for that user exactly as on a normal visit. ## Why drift matters A targeted `replace` updates the row it names and nothing else. On the chores page, ticking a chore also changes the "3 of 10 done" summary, the "all done" banner and perhaps a household leaderboard. Every one of those needs its own stream, and missing one leaves the page inconsistent until the next reload. A refresh re-renders all of them by construction. ## Why cost matters The refresh model multiplies work by the number of open tabs. A rough check: 1. Estimate concurrent viewers per stream (a household: a few; a public status board: hundreds). 2. Estimate changes per minute on that stream. 3. Multiply by the page's render time; that is the extra server load refreshes add. Server-side debouncing (0.5 s per stream, request id and thread in turbo-rails) and Turbo's 150 ms client debounce soften bursts, and the tab that made the change skips its own refresh, but neither reduces the per-viewer fan-out. ## Questions to ask about a screen 1. How many people watch the same stream at once, at peak? 2. How often does the underlying data change while they watch? 3. How long does the page take to render, and could slow parts move into frames? 4. Does the markup depend on who is looking (permissions, assignments, preferences)? 5. How many derived elements (counts, totals, banners) change with each record? High answers to 1-3 push towards targeted streams; high answers to 4-5 push towards refreshes. ## A decision rule - **Prefer refreshes** for collaborative screens with few concurrent viewers, cheap pages, per-user markup, or many derived elements: chore lists, project boards, admin tables. - **Prefer targeted streams** for high fan-out or high frequency (live feeds, chat, public dashboards), for expensive pages, or where an update must not disturb what the user is doing. - **Mix them** where it helps: refreshes for the page, a targeted `append` for a high-volume activity feed inside it, or a frame with `refresh="morph"` that reloads only its own region during a refresh. Whichever is chosen, the decision is about who renders and how often, not about which is more modern: both are first-class in turbo-rails.
- A chore row shows 'assigned to you' for the current member. Which broadcast style renders it correctly, and why?Refresh broadcasts. Each tab re-fetches the page in its own request with its own session, so the server knows the viewer. A targeted broadcast renders the partial once through `ApplicationController.render`, outside any request, so there is no current user to compare against.
- How can you keep refreshes but avoid reloading an expensive part of the chores page each time?Move that region into a lazily loaded `<turbo-frame>` with `src`, or mark it `data-turbo-permanent` if it should never change on refresh. A frame with `refresh="morph"` and `src` is reloaded on its own during a page refresh, which keeps its content but still costs a request.
- Refresh broadcasts send less data, so they are always cheaper for the server.
- Targeted stream partials can use current_user like any other view.
- A replace stream for one chore also updates the page's done counter automatically.
- Morphing refreshes are deprecated in favour of targeted streams in turbo-rails 2.0.
- Refreshes and targeted streams cannot be combined on the same page.