Presence in Convex
It's written in such a way that subscription updates within a "room" are only sent whenever someone joins/leaves, not on every heartbeat. It also shares the same cached subscription for everyone in the room so only issues a single database read. I've set it up so it marks a user as offline immediately whenever they lose visibility state or close the browser tab, but it also disconnects them after a timeout if the connection fails somehow.
Demo: https://presence.previews.convex.dev/
Source code: https://github.com/get-convex/presence/

