Creating OnDisconnect operations
Callref.onDisconnect() to get an OnDisconnect object, then chain a write method:
Methods
.set(value)
Sets a value when the client disconnects:
.update(values)
Performs a shallow merge when the client disconnects:
.remove()
Deletes data when the client disconnects:
.setWithPriority(value, priority)
Sets a value with a priority when the client disconnects:
.cancel()
Cancels a previously registered OnDisconnect operation:
Presence system
The most common use case for OnDisconnect is a presence system: tracking which users are currently online. Here’s a complete implementation:The order matters. Register the OnDisconnect operation before writing your online status. This avoids a race condition where you set
online: true but the connection drops before the OnDisconnect is registered.Why subscribe to .info/connected?
You might wonder why we don’t just run the setup once. The reason is reconnection. If the client loses its connection and reconnects, previous OnDisconnect operations are cleared. By listening to .info/connected, you re-register the OnDisconnect handler every time the connection is re-established.

