Stay organized with collections
Save and categorize content based on your preferences.
Methods
setWithPriority
- setWithPriority
(
value
:
any
,
priority
:
number
|
string
|
null
,
onComplete
?
:
(
a
:
Error
|
null
)
=>
any
)
:
Promise
<
any
>
-
Parameters
-
value: any
-
priority: number | string | null
-
Optional onComplete: (a: Error | null) => any
Returns Promise<any>
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-07-27 UTC.
[null,null,["Last updated 2022-07-27 UTC."],[],[],null,["- [firebase](/docs/reference/js/v8/firebase).\n- [database](/docs/reference/js/v8/firebase.database).\n- OnDisconnect \nThe `onDisconnect` class allows you to write or clear data when your client\ndisconnects from the Database server. These updates occur whether your\nclient disconnects cleanly or not, so you can rely on them to clean up data\neven if a connection is dropped or a client crashes.\n\nThe `onDisconnect` class is most commonly used to manage presence in\napplications where it is useful to detect how many clients are connected and\nwhen other clients disconnect. See\n[Enabling Offline Capabilities in JavaScript](https://firebase.google.com/docs/database/web/offline-capabilities) for more information.\n\nTo avoid problems when a connection is dropped before the requests can be\ntransferred to the Database server, these functions should be called before\nwriting any data.\n\nNote that `onDisconnect` operations are only triggered once. If you want an\noperation to occur each time a disconnect occurs, you'll need to re-establish\nthe `onDisconnect` operations each time you reconnect.\n\nIndex\n\nMethods\n\n- [cancel](/docs/reference/js/v8/firebase.database.OnDisconnect#cancel)\n- [remove](/docs/reference/js/v8/firebase.database.OnDisconnect#remove)\n- [set](/docs/reference/js/v8/firebase.database.OnDisconnect#set)\n- [setWithPriority](/docs/reference/js/v8/firebase.database.OnDisconnect#setwithpriority)\n- [update](/docs/reference/js/v8/firebase.database.OnDisconnect#update)\n\nMethods\n\ncancel\n\n- cancel ( onComplete ? : ( a : [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null ) =\\\u003e any ) : Promise \\\u003c any \\\u003e\n- Cancels all previously queued `onDisconnect()` set or update events for this\n location and all children.\n\n If a write has been queued for this location via a `set()` or `update()` at a\n parent location, the write at this location will be canceled, though writes\n to sibling locations will still occur.\n\n example\n :\n\n var ref = firebase.database().ref(\"onlineState\");\n ref.onDisconnect().set(false);\n // ... sometime later\n ref.onDisconnect().cancel();\n\n\n Parameters\n -\n\n Optional onComplete: (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null) =\\\u003e any \n An optional callback function that will\n be called when synchronization to the server has completed. The callback\n will be passed a single parameter: null for success, or an Error object\n indicating a failure.\n -\n - (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null): any\n\n \u003c!-- --\u003e\n\n -\n\n Parameters\n -\n\n a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null\n\n Returns any\n\n Returns Promise\\\u003cany\\\u003e\n\n Resolves when synchronization to the server\n is complete.\n\nremove\n\n- remove ( onComplete ? : ( a : [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null ) =\\\u003e any ) : Promise \\\u003c any \\\u003e\n- Ensures the data at this location is deleted when the client is disconnected\n (due to closing the browser, navigating to a new page, or network issues).\n\n Parameters\n -\n\n Optional onComplete: (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null) =\\\u003e any \n An optional callback function that will\n be called when synchronization to the server has completed. The callback\n will be passed a single parameter: null for success, or an Error object\n indicating a failure.\n -\n - (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null): any\n\n \u003c!-- --\u003e\n\n -\n\n Parameters\n -\n\n a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null\n\n Returns any\n\n Returns Promise\\\u003cany\\\u003e\n\n Resolves when synchronization to the server\n is complete.\n\nset\n\n- set ( value : any , onComplete ? : ( a : [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null ) =\\\u003e any ) : Promise \\\u003c any \\\u003e\n- Ensures the data at this location is set to the specified value when the\n client is disconnected (due to closing the browser, navigating to a new page,\n or network issues).\n\n `set()` is especially useful for implementing \"presence\" systems, where a\n value should be changed or cleared when a user disconnects so that they\n appear \"offline\" to other users. See\n [Enabling Offline Capabilities in JavaScript](https://firebase.google.com/docs/database/web/offline-capabilities) for more information.\n\n Note that `onDisconnect` operations are only triggered once. If you want an\n operation to occur each time a disconnect occurs, you'll need to re-establish\n the `onDisconnect` operations each time.\n\n example\n :\n\n var ref = firebase.database().ref(\"users/ada/status\");\n ref.onDisconnect().set(\"I disconnected!\");\n\n\n Parameters\n -\n\n value: any \n The value to be written to this location on\n disconnect (can be an object, array, string, number, boolean, or null).\n -\n\n Optional onComplete: (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null) =\\\u003e any \n An optional callback function that\n will be called when synchronization to the Database server has completed.\n The callback will be passed a single parameter: null for success, or an\n `Error` object indicating a failure.\n -\n - (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null): any\n\n \u003c!-- --\u003e\n\n -\n\n Parameters\n -\n\n a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null\n\n Returns any\n\n Returns Promise\\\u003cany\\\u003e\n\n Resolves when synchronization to the\n Database is complete.\n\nsetWithPriority\n\n- setWithPriority ( value : any , priority : number \\| string \\| null , onComplete ? : ( a : [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null ) =\\\u003e any ) : Promise \\\u003c any \\\u003e\n- Ensures the data at this location is set to the specified value and priority\n when the client is disconnected (due to closing the browser, navigating to a\n new page, or network issues).\n\n Parameters\n -\n\n value: any\n -\n\n priority: number \\| string \\| null\n -\n\n Optional onComplete: (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null) =\\\u003e any\n -\n - (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null): any\n\n \u003c!-- --\u003e\n\n -\n\n Parameters\n -\n\n a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null\n\n Returns any\n\n Returns Promise\\\u003cany\\\u003e\n\nupdate\n\n- update ( values : Object , onComplete ? : ( a : [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null ) =\\\u003e any ) : Promise \\\u003c any \\\u003e\n- Writes multiple values at this location when the client is disconnected (due\n to closing the browser, navigating to a new page, or network issues).\n\n The `values` argument contains multiple property-value pairs that will be\n written to the Database together. Each child property can either be a simple\n property (for example, \"name\") or a relative path (for example, \"name/first\")\n from the current location to the data to update.\n\n As opposed to the `set()` method, `update()` can be use to selectively update\n only the referenced properties at the current location (instead of replacing\n all the child properties at the current location).\n\n See more examples using the connected version of\n [`update()`](/docs/reference/js/v8/firebase.database.Reference#update).\n\n example\n :\n\n var ref = firebase.database().ref(\"users/ada\");\n ref.update({\n onlineState: true,\n status: \"I'm online.\"\n });\n ref.onDisconnect().update({\n onlineState: false,\n status: \"I'm offline.\"\n });\n\n\n Parameters\n -\n\n values: Object \n Object containing multiple values.\n -\n\n Optional onComplete: (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null) =\\\u003e any \n An optional callback function that will\n be called when synchronization to the server has completed. The\n callback will be passed a single parameter: null for success, or an Error\n object indicating a failure.\n -\n - (a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null): any\n\n \u003c!-- --\u003e\n\n -\n\n Parameters\n -\n\n a: [Error](/docs/reference/js/v8/firebase.functions.HttpsError#error) \\| null\n\n Returns any\n\n Returns Promise\\\u003cany\\\u003e\n\n Resolves when synchronization to the\nDatabase is complete."]]
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 writing any data.
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 theonDisconnect
operations each time you reconnect.