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.