Firebase: when onDisconnect event fire?

There are two cases that may happen when the client and server are disconnected:

  1. the client explicitly disconnects from the server
  2. the client simply disappears

When you kill the app, you are triggering an explicit disconnect. In that case the client will send a signal to the server that it is disconnecting and the server will immediately execute the onDisconnect callback.

When you switch off wifi, your app doesn't get a chance to tell the server that it is disconnecting. In that case the onDisconnect will fire once the server detects that the client is gone. This may take a few minutes, depending on how the time-outs for sockets are configured. So in that case you just have to be a bit more patient.

It this interval is not granular enough for you, the best you can do is updates a so-called keep-alive value in the database at regular, shorter intervals. That way you can see when the client was most recently active, and act on that if it was too long ago.