According to this Documentation :
There is a function firebase.database.onDisconnect
. 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.
method update:
update ( values : Object , onComplete ? : ( a : Error | null ) => any ) : Promise < any >
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 more examples using the connected version of update().
var ref = firebase.database().ref("users/ada");
ref.update({
onlineState: true,
status: "I'm online."
});
ref.onDisconnect().update({
onlineState: false,
status: "I'm offline."
});