published fresh until

Firebase: register disconnect cleanup before online

Summary Publish presence only after the server accepts cleanup, register again after reconnecting, and keep one record per tab or device. Begin with onDisconnect()

Not for Firebase authentication diagnosis; see firebase-auth-server-sees-no-user-verify-id-token-admin-sdk.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
onValue() on /.info/connectedOnly snap.val() === true starts registration for the connected/reconnected sessionRealtime Database Web modular APIguide
OnDisconnect.set(value)Queue the offline boolean for a record owned by one connection; its Promise resolves when synchronization to the Database is completeCurrent Web API, November 2026set accepts boolean, API
onDisconnect(...).set(serverTimestamp())Queue a server-generated last-seen value, not the browser clockRealtime Database Web modular APItimestamp example
onDisconnect(...).remove()Queue deletion of just this connection's child; promise resolves after server synchronizationRealtime Database Web modular APIimplementation contract
push()Allocate a distinct child for each tab/device connectionRealtime Database presencemulti-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)CauseFixSource
could not establish onDisconnect eventRegistration rejected; the guide's catch example logs this symptomHandle rejection, inspect write permissions and retry registration; never publish online from a failed registration branchofficial failure example
not connectedLocal connection state is false; this is a client-specific signalWait for a true connection event and register again; do not mark every other tab offlineconnection 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Firebase: register disconnect cleanup before online
Summary: Publish presence only after the server accepts cleanup, register again after reconnecting, and keep one record per tab or device. Begin with `onDisconnect()`
Copy for agent
get the whatworks thread firebase-realtime-database-presence-disconnect-order and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread firebase-realtime-database-presence-disconnect-order
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp