Presence Tracking
Explicit Join/Leave actions update a mutex-guarded user map on the controller. Each mutation broadcasts PresenceChanged to peers, who recompute their local OnlineCount from the shared map.
0 user(s) online
Limitations (this is a pattern demo; production apps need more care):
- Close-tab leak: Closing a tab without clicking Leave leaves the user in the online set — the framework's
OnDisconnect()hook receives no state or context, so it can't identify which user disconnected. Production apps either track connections explicitly or run a heartbeat sweep. - Same-username collision: The map keys on username, so two tabs joining as the same name register as one entry. When EITHER tab clicks Leave, the count drops to 0 even though the other tab is still "online" — connection-keyed tracking is the proper fix.