module ApplicationCable
class Connection < ActionCable::Connection::Base
identified_by :current_member
def connect
self.current_member = env['warden']&.user || reject_unauthorized_connection
end
end
end
class RoomChannel < ApplicationCable::Channel
def subscribed
@room = Room.find(params[:room_id])
stream_for @room
Presence.track_join(@room.id, current_member.id)
broadcast_presence
end
def unsubscribed
Presence.track_leave(@room.id, current_member.id)
broadcast_presence
end
private
def broadcast_presence
Turbo::StreamsChannel.broadcast_replace_to(
@room,
target: 'room_presence',
partial: 'rooms/presence',
locals: { room: @room, member_ids: Presence.online_member_ids(@room.id) }
)
end
end
<div id="room_presence" class="text-sm text-gray-600">
Online: <%= Member.where(id: member_ids).pluck(:nickname).join(', ') %>
</div>
Presence is usually overkill, but for collaboration features it’s valuable: show who’s online in a room. I identify connections with current_member in ApplicationCable::Connection, then in a channel I broadcast updates when members subscribe/unsubscribe. The UI subscribes with turbo_stream_from @room and renders a partial listing online members. I keep the data minimal and ephemeral: store online member IDs in Redis or a simple in-memory store (depending on deployment), and never treat presence as security. The Turbo approach is nice because updates are HTML fragments, so styling is consistent and you don’t build a JSON protocol. The main gotcha is scale: presence can be chatty, so throttle broadcasts and keep the partial fast.
Related snips
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
System test: asserting Turbo Stream responses
class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :comments, dependent: :destroy
scope :published, -> { where.not(published_at: nil).where('published_at <= ?', Time.current) }
scope :draft, -> { where(published_at: nil) }
ActiveRecord scopes for reusable query logic
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
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
require "csv"
class PeopleCsvStream
include Enumerable
HEADERS = %w[id full_name email signed_up_at plan].freeze
Resilient CSV Export as a Streamed Response
<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)
Share this code
Here's the card — post it anywhere.