Methods

cancel

cancel(onComplete) returns Promise containing void

Cancels all previously queued onDisconnect() set or update events for this location and all children.

If a write has been queued for this location via a set() or update() at a parent location, the write at this location will be canceled, though all other siblings will still be written.

Parameter

onComplete

Optional

function(nullable Error)

An optional callback function that will be called when synchronization to the server has completed. The callback will be passed a single parameter: null for success, or an Error object indicating a failure.

Returns

non-null Promise containing void Resolves when synchronization to the server is complete.

Example

var ref = admin.database().ref("onlineState");
ref.onDisconnect().set(false);
// ... sometime later
ref.onDisconnect().cancel();

remove

remove(onComplete) returns Promise containing void

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).

Parameter

onComplete

Optional

function(nullable Error)

An optional callback function that will be called when synchronization to the server has completed. The callback will be passed a single parameter: null for success, or an Error object indicating a failure.

Returns

non-null Promise containing void Resolves when synchronization to the server is complete.

set

set(value, onComplete) returns Promise containing void

Ensures the data at this location is set to the specified value when the client is disconnected (due to closing the browser, navigating to a new page, or network issues).

set() is especially useful for implementing "presence" systems, where a value should be changed or cleared when a user disconnects so that they appear "offline" to other users. See Enabling Offline Capabilities in JavaScript for more information.

Note that onDisconnect operations are only triggered once. If you want an operation to occur each time a disconnect occurs, you'll need to re-establish the onDisconnect operations each time.

Parameter

value

any type

The value to be written to this location on disconnect (can be an object, array, string, number, boolean, or null).

onComplete

Optional

function(nullable Error)

An optional callback function that will be called when synchronization to the Database server has completed. The callback will be passed a single parameter: null for success, or an Error object indicating a failure.

Returns

non-null Promise containing void Resolves when synchronization to the Database is complete.

Example

var ref = admin.database().ref("users/ada/status");
ref.onDisconnect().set("I disconnected!");

setWithPriority

setWithPriority(value, priority, onComplete) returns Promise containing void

Ensures the data at this location is set to the specified value and priority when the client is disconnected (due to closing the browser, navigating to a new page, or network issues).

Parameter

value

any type

priority

(number, string, or null)

onComplete

Optional

function(nullable Error)

Returns

non-null Promise containing void 

update

update(values, onComplete) returns Promise containing void

Writes multiple values at this location when the client is disconnected (due to closing the browser, navigating to a new page, or network issues).

The values argument contains multiple property-value pairs that will be written to the Database together. Each child property can either be a simple property (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 update only the referenced properties at the current location (instead of replacing all the child properties at the current location).

See admin.database.Reference#update for examples of using the connected version of update.

Parameter

values

Object

Object containing multiple values.

Value must not be null.

onComplete

Optional

function(nullable Error)

An optional callback function that will be called when synchronization to the server has completed. The callback will be passed a single parameter: null for success, or an Error object indicating a failure.

Returns

non-null Promise containing void Resolves when synchronization to the Database is complete.

Example

var ref = admin.database().ref("users/ada");
ref.update({
   onlineState: true,
   status: "I'm online."
});
ref.onDisconnect().update({
  onlineState: false,
  status: "I'm offline."
});