ruby erb 41 lines · 3 tabs

Turbo Stream broadcasts for real-time collaboration

Jordan Lee Jan 2026
3 tabs
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :author, class_name: 'User'

  # Automatically broadcast changes to all subscribers of this post
  after_create_commit -> { broadcast_append_to post, target: "comments" }
  after_update_commit -> { broadcast_replace_to post }
  after_destroy_commit -> { broadcast_remove_to post }

  validates :body, presence: true
end
3 files · ruby, erb Explain with highlit

Broadcasting Turbo Streams via Action Cable enables real-time collaborative features without polling. When a model is created, updated, or destroyed, I broadcast the change to all subscribed users using broadcasts_to or manual broadcast_* methods. Subscribers receive Turbo Stream fragments that update their DOM automatically. This pattern powers collaborative editing, live notifications, real-time dashboards, and multiplayer features. The key is scoping broadcasts appropriately—I use stream_for to create user-specific or resource-specific channels. I also include the current user's ID in the broadcast payload so their UI doesn't duplicate the update they just made. Proper authorization ensures users only subscribe to streams they're allowed to see.


Related snips

ruby
class CommentsController < ApplicationController
  before_action :set_post

  def create
    @comment = @post.comments.build(comment_params)

System test: asserting Turbo Stream responses

rails hotwire turbo
by codesnips 4 tabs
ruby
class PostsController < ApplicationController
  def index
    @posts = Post.includes(:author)
                 .order(created_at: :desc)
                 .page(params[:page])
                 .per(10)

Turbo Frames: infinite scroll with lazy-loading frame

rails turbo hotwire
by codesnips 4 tabs
erb
<form data-controller="query-sync" data-action="change->query-sync#apply">
  <select name="status" class="rounded border p-2">
    <option value="">Any</option>
    <option value="open">Open</option>
    <option value="closed">Closed</option>
  </select>

Filter UI that syncs query params via Stimulus (no front-end router)

rails hotwire stimulus
by Henry Kim 2 tabs
ruby
class TasksController < ApplicationController
  def destroy
    @task = Task.find(params[:id])
    @task.destroy!

    respond_to do |format|

Remove deleted items instantly with turbo_stream.remove

rails hotwire turbo
by Henry Kim 2 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["form"]
  static values = { delay: { type: Number, default: 250 } }

Debounced live search with Stimulus + Turbo Streams

rails hotwire stimulus
by codesnips 4 tabs
ruby
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_member

    def connect
      self.current_member = env['warden']&.user || reject_unauthorized_connection

Presence indicator with ActionCable + Turbo Streams

rails hotwire turbo
by Henry Kim 3 tabs

Share this code

Here's the card — post it anywhere.

Turbo Stream broadcasts for real-time collaboration — share card
Link copied