OnDisconnect
OnDisconnect lets you register operations that run on the server the moment a client disconnects. You set them up while connected, and the server holds onto them. When the connection drops — whether the user closes the tab, the network fails, or the device crashes — the server executes them immediately.Available operations
OnDisconnect supports the same write operations you use elsewhere:- set — Overwrite the data at the path.
- update — Merge values into the existing data.
- remove — Delete the data at the path.
- cancel — Cancel any previously queued OnDisconnect operations for this path.
OnDisconnect operations survive brief network interruptions. The server only executes them when it confirms the connection is truly gone — not on every momentary blip. If the client reconnects before the server times out the connection, the disconnect operations remain queued and are not fired.
.info/connected
Lark provides a special read-only path, .info/connected, that tells you whether the client is currently connected to the server. It’s true when connected and false when disconnected.
Subscribe to it to show a connection indicator in your UI — a banner, an icon, a “reconnecting…” message.
Building a presence system
The classic pattern for presence combines OnDisconnect,.info/connected, and regular writes:
- Subscribe to
.info/connected. - When the connection is established (or re-established), register an OnDisconnect to set the user offline and write a
lastSeentimestamp. - Once the OnDisconnect is confirmed queued on the server, set the user online.
- If the connection drops, the server executes the OnDisconnect — setting the user offline.
- When the client reconnects, the cycle repeats.
Multiple connections per user
OnDisconnect operations are tied to a specific connection. If a user opens your app in two tabs, each tab has its own connection and its own OnDisconnect handlers. Closing one tab fires that tab’s disconnect operations — potentially setting the user offline while they’re still active in the other tab. Design your presence data model to handle this if needed (for example, by tracking connection count instead of a boolean).Next steps
Lark SDK OnDisconnect
Full API reference and code examples for OnDisconnect and presence in the Lark SDK.

