Handling Ghost Users in Realtime Presence Systems
A Node.js service should manage presence as a short lease owned by one editor session, not as a fact that a disconnect event must erase, according to Dev.to. This approach ensures that presence is not incorrectly…

When real-time presence shows ghost users after a network drop, the presence should be treated as a short lease owned by one editor session, not as a fact that a disconnect event must erase. A Node.js service should expire the lease unless the same authenticated session renews it. Disconnect events can speed up cleanup, but they cannot be the source of truth. This approach ensures that presence is a temporary claim rather than a durable application data point (Dev.to, 2023). [1]
A network drop differs from a graceful close because the client cannot send a final message when Wi-Fi vanishes, a laptop sleeps, or a process is killed. WebRTC exposes ICE and peer connection state transitions, but these states describe the local connection view and do not create a globally reliable leave event. This means that a ghost is normal during the bounded expiry window and a bug after it (Dev.to, 2023). [1]
The tempting design is a map keyed by userId, followed by delete(userId) on socket close. However, this design is ambiguous as soon as a reporter opens the same story in two tabs. Closing tab A can erase tab B, while a missing close can preserve both forever. Scope the credential more narrowly by using a random presence token restricted to one document, one user, one session, and a short expiry (Dev.to, 2023). [1]
The source discusses a Node.js service managing real-time presence in a collaborative editing environment, specifically within a newsroom. The system tracks presence through leases, which are short-lived claims tied to an editor's session. Each lease is uniquely identified by a combination of document ID, user ID, and session ID, and is associated with an expiry time. The lease duration is set to thirty seconds in the example, though this can be adjusted based on product requirements. The system is designed to handle network drops by relying on lease expiration rather than disconnect events, as the latter are unreliable in identifying when a user has truly left the session. This approach prevents ghost users from appearing in the UI after a network disruption, as the lease will expire if not renewed by the client within the specified time window. The system ensures that presence is treated as a temporary claim, not a durable state, by separating it from in-app notifications, which are stored separately and managed under their own authorization rules. [1]
Advertisement
The system's design includes a map of leases managed in memory, with a key generated from document ID, user ID, and session ID. Renewing a lease extends its expiry time, while expiring a lease removes it from the map. The source provides a code sketch where the `renewPresence` function adds or updates a lease with a new expiry time, and the `expirePresence` function removes any lease that has expired based on the current time. The `leavePresence` function allows a lease to be manually removed by the client. This implementation ensures that the server maintains the correct state of active sessions and that the UI reflects only unexpired leases. The system also emphasizes the use of monotonic server time over browser wall clocks to ensure consistency in lease management. This is crucial for accurate lease expiration and to avoid discrepancies caused by client-side clock variations. [1]
The source highlights the importance of testing lease management under various failure scenarios, such as network interruptions, delayed renewals, and race conditions between lease renewal and expiry. It suggests using a fake clock to simulate different timing scenarios, including a 30-second lease renewed every 10 seconds, then introducing a 31-second gap to test the system's behavior. The system must not extend the expiry of a lease based on a late renewal, nor should it allow an old session's leave event to delete a new session's lease. The source also emphasizes the importance of logging detailed information, such as hashed session identifiers, document identifiers, server time, and expiry transitions, to aid in debugging. This approach ensures that the system can be monitored for active leases, expiry lag, and reconnects, and that the lease expiration logic remains correct even under high load or network instability. [1]
The source describes the system's limitations and trade-offs, particularly in scaling. It notes that process-local maps used for managing leases can cause inconsistencies when the Node.js service runs on multiple instances, and suggests moving leases to a shared store that supports atomic conditional updates and expiry. This would ensure consistency across all instances of the service. The source also outlines the separation of three distinct budgets—renewal traffic, fan-out traffic, and durable notification writes—to manage the load on the system. Presence updates can be coalesced, and cursor movements can be dropped if necessary, but mention notifications must be persisted separately. This design choice allows the system to handle busy scenarios, such as a story with 40 editors, without overwhelming the service with unnecessary updates. The trade-off is deliberate staleness in presence updates to maintain performance and reliability in the face of network or system failures. [1]
The source provides specific examples of scenarios that must be tested to ensure the system's correctness. These include a 30-second lease renewed every 10 seconds, followed by a 31-second gap to simulate a failure in renewal. It also suggests testing a late renewal, duplicate renewal, an old-session leave, and two tabs for one user to ensure that the system correctly handles these edge cases. The system must not allow a delayed close event to delete a lease that is still valid, nor should it extend the expiry of a lease based on a late renewal. The source also emphasizes the importance of using a single clock domain for server decisions and treating browser wall clocks as evidence, not authoritative time sources. This ensures that lease expiration and renewal are consistent and reliable, even in the presence of network disruptions or client-side failures. [1]
Advertisement
When real-time presence shows ghost users after a network drop, the presence should be treated as a short lease owned by one editor session, not as a fact that a disconnect event must erase (Dev.to). A network drop differs from a graceful close because the client cannot send a final message when Wi-Fi vanishes, a laptop sleeps, or a process is killed (Dev.to). The tempting design is a map keyed by userId, followed by delete(userId) on socket close (Dev.to). [1]
WebRTC exposes ICE and peer connection state transitions, including disconnected, failed, and closed, but those states describe the local connection view. They do not create a globally reliable leave event (Dev.to). That gives the first debugging expectation: a ghost is normal during the bounded expiry window and a bug after it (Dev.to). If the UI promises instant removal after every link loss, the promise is wrong (Dev.to). [1]
When real-time presence shows ghost users after a network drop, the presence should be treated as a short lease owned by one editor session, not as a fact that a disconnect event must erase (Dev.to). This distinction is crucial because a network drop differs from a graceful close. The client cannot send a final message when Wi-Fi vanishes, a laptop sleeps, or a process is killed. As such, relying solely on disconnect events can lead to incorrect assumptions about user activity. [1]
WebRTC exposes ICE and peer connection state transitions, including disconnected, failed, and closed, but those states describe the local connection view (Dev.to). These states do not create a globally reliable leave event, which means that a ghost user may be normal during the bounded expiry window. If the UI promises instant removal after every link loss, that promise is wrong, as the contract between the system and the user must be clearly defined. [1]
Advertisement
A tempting design is a map keyed by userId, followed by delete(userId) on socket close. However, this design is ambiguous as soon as a reporter opens the same story in two tabs. Closing one tab can erase the presence of the other, leading to incorrect behavior. To avoid this, each authenticated editor session should be given a random presence token restricted to one document, one user, and one session, with a short expiry (Dev.to). [1]
Sources
Prepared with AI assistance from the cited sources.
The Cali Reporter newsletter
The stories worth your time.
California, the country, and what comes next. Join our free newsletter for reporting that keeps you informed.
More in Technology
- Modern Web Guidance 1.0 and Browser Compatibility
Modern Web Guidance 1.0, announced by Chrome for Developers on November 10, introduces browser compatibility data to assist coding agents in building web features. The initiative emphasizes the importance of fallback…
- Apache mod_http2 Vulnerability Detection with Nmap
A new Nmap scripting module has been developed to detect the Apache mod_http2 vulnerability (CVE-2026-119618), addressing the challenges of identifying affected systems accurately.
- Securing Remote Vendor Access in Industrial Networks
Best practices for managing remote vendor access in operational technology (OT) networks emphasize controlled access points, multifactor authentication, and time-bound sessions to reduce cybersecurity risks.
