class Notification < ApplicationRecord
belongs_to :member
after_create_commit -> { broadcast_prepend_to stream_name, target: "notifications", partial: "notifications/notification", locals: { notification: self } }
after_update_commit -> { broadcast_replace_to stream_name, partial: "notifications/notification", locals: { notification: self } }
def stream_name
[member, :notifications]
end
end
<%= turbo_stream_from [current_member, :notifications] %>
<div id="notifications"><%= render @notifications %></div>
When updates can happen from multiple places, model-level broadcasts keep the UI consistent across tabs without sprinkling stream logic in controllers. Use after-commit hooks so broadcasts only occur once the write is durable.