Перейти к консоли

The onDisconnect class allows you to write or clear data when your client disconnects from the Database server. These updates occur whether your client disconnects cleanly or not, so you can rely on them to clean up data even if a connection is dropped or a client crashes.

The onDisconnect class is most commonly used to manage presence in applications where it is useful to detect how many clients are connected and when other clients disconnect. See Enabling Offline Capabilities in JavaScript for more information.

To avoid problems when a connection is dropped before the requests can be transferred to the Database server, these functions should be called before any data is written.

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 you reconnect.

Index

Methods

cancel

  • cancel(onComplete?: function): Promise<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.

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

    Parameters

    • Optional onComplete: function

      An optional callback function that is 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.

        • (a: Error | null): any
        • Parameters

          • a: Error | null

          Returns any

    Returns Promise<void>

    Resolves when synchronization to the server is complete.

remove

  • remove(onComplete?: function): Promise<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).

    Parameters

    • Optional onComplete: function

      An optional callback function that is 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.

        • (a: Error | null): any
        • Parameters

          • a: Error | null

          Returns any

    Returns Promise<void>

    Resolves when synchronization to the server is complete.

set

  • set(value: any, onComplete?: function): Promise<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.

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

    Parameters

    • value: any

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

    • Optional onComplete: function

      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.

        • (a: Error | null): any
        • Parameters

          • a: Error | null

          Returns any

    Returns Promise<void>

    A promise that resolves when synchronization to the database is complete.

setWithPriority

  • setWithPriority(value: any, priority: number | string | null, onComplete?: function): Promise<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).

    Parameters

    • value: any

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

    • priority: number | string | null
    • Optional onComplete: function

      An optional callback function that is 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.

        • (a: Error | null): any
        • Parameters

          • a: Error | null

          Returns any

    Returns Promise<void>

    A promise that resolves when synchronization to the database is complete.

update

  • update(values