Turbo Streams: Push Live HTML Updates Without Custom JavaScript
Turbo Streams push partial HTML over WebSockets to update targeted DOM elements without custom JavaScript or page reloads.
22 Aug 2025, 20:39 UTC

A comment appears for one user, but not the other
You ship a comment feature. User A posts a reply. User B, watching the same thread, sees nothing until they refresh. You could poll the server every few seconds, but that wastes requests and still feels sluggish. You could write custom WebSocket handlers in JavaScript, but now you are maintaining client-side state synchronization alongside your server-side templates.
Turbo Streams move that logic to the server. The controller renders a fragment of HTML wrapped in a <turbo-stream> tag and broadcasts it over a WebSocket. Every connected browser applies the fragment to a specific element identified by ID or CSS selector. No custom JavaScript is required to move the data from the socket into the DOM.
What a Turbo Stream message looks like
A Turbo Stream is an HTML message sent over a WebSocket connection. It uses one of several actions to modify the page:
append— insert content inside the target elementprepend— insert content at the start of the target elementreplace— swap the target element with new contentremove— delete the target elementupdate— replace the inner HTML of the target element
The target attribute identifies where the browser should apply the change. Turbo Streams work alongside Turbo Drive and Turbo Frames, but they are independent: you can use them for real-time partial updates without changing how links or forms navigate.
Worked example: broadcast a new comment
This example assumes a Rails-like stack using Action Cable as the WebSocket adapter. Other stacks can implement the same pattern with any WebSocket server that can send <turbo-stream> messages.
The page contains a container with a stable ID:
<div id="comments">
<!-- existing comments rendered here -->
</div>
When a comment is saved, the server renders a stream fragment:
<turbo-stream action="append" target="comments">
<template>
<div id="comment_42">
This is the new comment body.
</div>
</template>
</turbo-stream>
In a controller, the broadcast might look like this:
def create
@comment = Comment.new(comment_params)
if @comment.save
broadcast_append_to "comments",
target: "comments",
partial: "comments/comment",
locals: { comment: @comment }
redirect_to thread_path(@comment.thread)
else
render :new
end
end
broadcast_append_to is illustrative; the exact helper or method depends on your framework and adapter. The important part is that the server emits a <turbo-stream> message after the database write, and every subscriber receives it over the same WebSocket channel.
User B’s browser receives the message, finds the element with ID comments, and appends the new comment markup. The page does not reload.
Where it breaks: silent misses and connection cost
- Missing targets fail silently. If the
targetID does not exist in the DOM, the browser typically does nothing and does not log an error by default. The UI can appear stuck while the server believes the update was delivered. - WebSockets are stateful. Each open connection consumes server memory and file descriptors. Under heavy load, connection management becomes a scaling concern that stateless HTTP endpoints do not have.
- Reordering and reconnection can desynchronize state. If messages arrive out of order or a client reconnects after a drop, the client and server views of the DOM can diverge unless updates are idempotent or keyed by stable IDs.
Turbo Streams are best suited for intermittent state changes—new comments, status updates, task completion—rather than high-frequency events like typing indicators.
Check it before you ship
- Open browser DevTools and open the WebSocket frames view (Network panel, filter by WS).
- Trigger the state-changing action from one browser.
- Confirm a frame arrives containing
<turbo-stream>. - Inspect the DOM in the second browser and verify the targeted element changed without a full page reload.
- On the server, run
tail -f log/development.logfrom the application directory. You should see WebSocket subscription and broadcast lines. In production, ensure you have permission to read the log file; high traffic can make tail output noisy and hard to interpret.
If no frame appears, check that the WebSocket adapter is mounted and the client is authenticated to the channel. If a frame arrives but nothing changes, verify the target ID exists and the stream tag is well-formed.
Start with one channel
Pick a single update that currently forces a refresh—such as a new comment or a changed task status—and broadcast it with Turbo Streams. Verify the frame and DOM update in DevTools before adding more channels. Turbo Streams are not a replacement for every interactive UI, but they let you push partial HTML to connected clients without writing custom WebSocket JavaScript.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.