actioncable-presence

Presence for Rails Action Cable

Who is here, and never a ghost.

Presence is the "3 people are viewing this" list: who has the page open right now. The Action Cable guide's pattern adds you on subscribed and removes you on unsubscribed, so when a server process dies or a laptop lid closes, that callback never runs and you stay listed forever as a ghost.

actioncable-presence owns every row by a leased process, so a killed worker's users leave within about 10 seconds, and a silent client is dropped within about 9.

Read the README

You'll be Cosmic vole. Share the room link with a friend, or open it in a second browser.

1 Kill the worker serving you

One button sends kill -9 to the Puma worker holding your WebSocket. No callbacks run. Puma respawns the worker, you reconnect, and your old connection's row is a ghost. A countdown shows when it clears.

2 Compare with the tutorial pattern

Next to it, the same room tracked the guide's way: insert on subscribe, delete on unsubscribe. After the kill, that list keeps your ghost until someone presses Reset.

3 Pull the network cable

"Simulate a dead network" stops your tab answering pings, like a closed laptop lid. The server notices within 9 seconds and everyone else sees you leave.

The whole server side

One concern on the channel. The client is a small reconciler with a React hook, or plain JS like this page uses.

class RoomChannel < ApplicationCable::Channel
  include ActionCable::Presence::Channel

  def subscribed
    stream_from "room:#{params[:id]}"
    join_presence(id: current_user.id, info: { name: current_user.name })
  end
end