Skip to content Skip to sidebar Skip to footer

Firebase: When OnDisconnect Event Fire?

I`m using Firebase-backend for my android applicaion. I'd like to build a users presence system for my chat. For this purpose I've taken the pattern from Firebase Guide final Fireb

Solution 1:

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.


Solution 2:

When user is exiting the app, you could call admin.database().goOffline() to explicitly disconnect from the firebase, which should trigger the onDisconnect callback. When user is returning to the app just call admin.database().goOnline() to reconnect with firebase.


Post a Comment for "Firebase: When OnDisconnect Event Fire?"