What this thread does
Do first: listen to onValue(ref(db, ".info/connected"), ...) and await disconnect-handler registration before writing an online marker for that connection.
Short answer
As of November 2026, Firebase JavaScript SDK 13.4.0, November 16, 2026 is the release baseline. Realtime Database executes registered disconnect operations on its server when it detects a closed/timed-out connection. They fire once, so every reconnection needs new registrations (OnDisconnect API). Disconnect operations clean up data even if a connection is dropped or a client crashes. Queue the disconnect operations before a user is marked online; use separate per-connection children for multiple tabs/devices (presence guide).
Choose / don't choose
- One connection's state: choose
onDisconnect(lastSeenRef).set(serverTimestamp())for last seen; the references are application-defined (last-seen example). - User presence across tabs: choose a child per connection and remove only that child on disconnect. When the connections value has no children the user is offline; don't let one departing tab set a shared user flag offline (sample).
- Connection observation: use the special connection state for this client's database connection; don't infer another client's connection from it. Michael Lehenbauer's answer describes the original signal; the current guide defines its modular API.
Not for: Firebase authentication diagnosis; see firebase-auth-server-sees-no-user-verify-id-token-admin-sdk.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
onValue() on /.info/connected | Only snap.val() === true starts registration for the connected/reconnected session | Realtime Database Web modular API | guide |
OnDisconnect.set(value) | Queue the offline boolean for a record owned by one connection; its Promise resolves when synchronization to the Database is complete | Current Web API, November 2026 | set accepts boolean, API |
onDisconnect(...).set(serverTimestamp()) | Queue a server-generated last-seen value, not the browser clock | Realtime Database Web modular API | timestamp example |
onDisconnect(...).remove() | Queue deletion of just this connection's child; promise resolves after server synchronization | Realtime Database Web modular API | implementation contract |
push() | Allocate a distinct child for each tab/device connection | Realtime Database presence | multi-connection sample |
Order of operations
1. Subscribe to the special connection path. On every true event, allocate the connection reference and register its disconnect removal. Register last-seen cleanup too if that is part of your presence contract. 2. Await all required registrations before online publication. The source documents that registration promises resolve after synchronization. A rejected registration must enter an error/retry path without calling the online set() (contract). 3. For a minimal multi-tab branch, the following illustrates application-owned references; catch failures around the awaited sequence and leave presence offline/unknown:
const con = push(myConnectionsRef);
await onDisconnect(con).remove();
await onDisconnect(lastOnlineRef).set(serverTimestamp());
await set(con, true);
The order combines the guide's per-connection design with its cleanup-before-online requirement (guide). 4. Repeat registration after each successful reconnect because handlers are one-shot (API). Associate asynchronous publication with the connection that registered it; abandon a stale attempt if a later connection event supersedes it. This last guard is application lifecycle policy. 5. Derive user presence from whether any connection child remains. A disconnect timestamp records a connection departure; while another tab remains online it is not necessarily the user's final offline time.
Known failures and fixes
Diagnostic: could not establish onDisconnect event
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
could not establish onDisconnect event | Registration rejected; the guide's catch example logs this symptom | Handle rejection, inspect write permissions and retry registration; never publish online from a failed registration branch | official failure example |
not connected | Local connection state is false; this is a client-specific signal | Wait for a true connection event and register again; do not mark every other tab offline | connection example, one-shot contract |
Missing
- Disconnect detection has no fixed instant deadline here. Server security is checked both when registering and when executing cleanup; later permission changes can invalidate it at the second check. Keep the required write permitted across that lifecycle (security behavior).
Checked
2026-11-15; Firebase JS 13.3.0 release notes, current presence/API/implementation references and dated Lehenbauer answer read. No Firebase operations executed.
Started from a post by @Firebase, packaged by the editor. Original post