TheonDisconnect class allows you to write or clear data when your clientdisconnects from the Database server. These updates occur whether yourclient disconnects cleanly or not, so you can rely on them to clean up dataeven if a connection is dropped or a client crashes.
TheonDisconnect class is most commonly used to manage presence inapplications where it is useful to detect how many clients are connected andwhen other clients disconnect. See Enabling Offline Capabilities in JavaScript for more information.
To avoid problems when a connection is dropped before the requests can betransferred to the Database server, these functions should be called beforewriting any data.
Note thatonDisconnect operations are only triggered once. If you want anoperation to occur each time a disconnect occurs, you'll need to re-establishtheonDisconnect operations each time you reconnect.
Index
Methods
cancel
- cancel(onComplete?: (a: Error |null) =>any):Promise<any>
Cancels all previously queued
onDisconnect()set or update events for thislocation and all children.If a write has been queued for this location via a
set()orupdate()at aparent location, the write at this location will be canceled, though writesto sibling locations will still occur.- example
var ref = firebase.database().ref("onlineState");ref.onDisconnect().set(false);// ... sometime laterref.onDisconnect().cancel();
Parameters
Optional onComplete:(a:Error |null) =>any
An optional callback function that willbe called when synchronization to the server has completed. The callbackwill be passed a single parameter: null for success, or an Error objectindicating a failure.
ReturnsPromise<any>
Resolves when synchronization to the serveris complete.
remove
- remove(onComplete?: (a: Error |null) =>any):Promise<any>
Ensures the data at this location is deleted when the client is disconnected(due to closing the browser, navigating to a new page, or network issues).
Parameters
Optional onComplete:(a:Error |null) =>any
An optional callback function that willbe called when synchronization to the server has completed. The callbackwill be passed a single parameter: null for success, or an Error objectindicating a failure.
ReturnsPromise<any>
Resolves when synchronization to the serveris complete.
set
- set(value: any, onComplete?: (a: Error |null) =>any):Promise<any>
Ensures the data at this location is set to the specified value when theclient is disconnected (due to closing the browser, navigating to a new page,or network issues).
set()is especially useful for implementing "presence" systems, where avalue should be changed or cleared when a user disconnects so that theyappear "offline" to other users. See Enabling Offline Capabilities in JavaScript for more information.Note that
onDisconnectoperations are only triggered once. If you want anoperation to occur each time a disconnect occurs, you'll need to re-establishtheonDisconnectoperations each time.- example
var ref = firebase.database().ref("users/ada/status");ref.onDisconnect().set("I disconnected!");
Parameters
value:any
The value to be written to this location ondisconnect (can be an object, array, string, number, boolean, or null).
Optional onComplete:(a:Error |null) =>any
An optional callback function thatwill be called when synchronization to the Database server has completed.The callback will be passed a single parameter: null for success, or an
Errorobject indicating a failure.
ReturnsPromise<any>
Resolves when synchronization to theDatabase is complete.
setWithPriority
- set
With Priority(value: any, priority: number |string |null, onComplete?: (a: Error |null) =>any):Promise<any> Ensures the data at this location is set to the specified value and prioritywhen the client is disconnected (due to closing the browser, navigating to anew page, or network issues).
Parameters
value:any
priority:number |string |null
Optional onComplete:(a:Error |null) =>any
ReturnsPromise<any>
update
- update(values: Object, onComplete?: (a: Error |null) =>any):Promise<any>
Writes multiple values at this location when the client is disconnected (dueto closing the browser, navigating to a new page, or network issues).
The
valuesargument contains multiple property-value pairs that will bewritten to the Database together. Each child property can either be a simpleproperty (for example, "name") or a relative path (for example, "name/first")from the current location to the data to update.As opposed to the
set()method,update()can be use to selectively updateonly the referenced properties at the current location (instead of replacingall the child properties at the current location).See more examples using the connected version of
update().- example
var ref = firebase.database().ref("users/ada");ref.update({onlineState:true,status:"I'm online."});ref.onDisconnect().update({onlineState:false,status:"I'm offline."});
Parameters
values:Object
Object containing multiple values.
Optional onComplete:(a:Error |null) =>any
An optional callback function that willbe called when synchronization to the server has completed. Thecallback will be passed a single parameter: null for success, or an Errorobject indicating a failure.
ReturnsPromise<any>
Resolves when synchronization to theDatabase is complete.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-07-27 UTC.