Firebase: When Ondisconnect Event Fire

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.

Firebase OnDisconnect Event

If the server-side onDisconnect does not for your use-case, the common approach is indeed to implement your own mechanism. Your proposed keep-alive approach still needs an arbiter to determine when a connection is dead. That is definitely possible, but would typically require a Cloud Function or more permanent listener.

Instead of that, I'd usually add a lastSeenTimestamp for each user/connection, and then have the clients that read those timestamps for everyone determine how fresh they want the connection to be before considering a user stale.

You could still use onDisconnect in that case to clean up the data, but you're not longer tied to the socket timeout interval of the server to mark the user as inactive in your clients.

How to detect onDisconnected event in firebase when wifi is suddenly turned off?

This is not how onDisconnect works. Try something like this:

var userRef = dbRef.child('..path to user object in database');
userRef.onDisconnect().update({
isOnline: false,
isBusy: false
});

Note that onDisconnect can sometimes take 1-2 minutes to actually do its stuff.

If you need something faster, you could periodically write something like

lastOnlineTimestamp: {'.sv': 'timestamp'}

in your user path and make the other users check how old this timestamp is.

Related docs:

https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect#update

Firebase Realtime Database - using onDisconnect for Presence

This is the expected behavior. It takes that amount of time to discern if the connection is actually lost, or instead just temporarily blocked for whatever reason. Basically, 60 seconds is the "timeout" until a read or write on the socket between your client app and the server is expected to complete.

Firebase onDisconnected called when closing the app by swiping it out

The onDisconnect handlers of your Firebase Database client are called:

  1. when the database client actively disconnects from the Firebase server
  2. when the server detects that the database client has disappeared (by the socket connection timing out)

Note that neither of these has anything to do with your application lifecycle, which seems to be what you are interested in.

If you want to change the database when your application is destroyed, you should probably listen to application lifecycle events to detect when the application exits.

Why is Firebase onDisconnect firing every hour?

I have no idea how this onDisconnect function is implemented. It would be helpful to explain how you determine the disconnection. Firebase Id tokens expire every hour and need to be refreshed (automatically refreshed after when calling any user method). If you are using real time database, they will try to refresh on expiration. You can add a listener in onAuthStateChanged when a token is issued and set a timer to automatically refresh the token (getToken(true)) before it expires, ensuring the disconnection does not trigger.

Also, the onDisconnect will fail during that disconnection because Firebase consider the token expired.

Firebase onDisconnect how to trigger other function

You can attach an onDisconnect() handler to any location in your database and remove or write data to that location.

But realize that onDisconnect() works by sending the write operation to the Firebase servers up front. So you can write any value, as long as that value can be calculated on the client when you attach the onDisconnect listener.



Related Topics



Leave a reply



Submit