Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase onDisconnect not 100% reliable now?

Tags:

firebase

I've been using Firebase extensively and still face only one real issue: onDisconnect isn't 100% reliable in my experience.

If you close a computer without closing the window first, or kill the browser, you sometime have the "garbage collector" getting your onDisconnect executed, sometimes it doesn't.

My question is the following: I just don't use /.connected for now, I basically use a simple

userRef.set('status', 1);
userRef.onDisconnect().update({ 'status' : 0 });

Is there anything wrong with this approach? Do we agree that the update parameters are passed to the server at the time the line is executed and not before window unload ?

NB: I happen to try to keep a multi-window status, using the following approach to keep the status at 1 if another window is closed:

userRef.child('status').on('value', function(snap) {
  if (snap.val() != 1) {
    userRef.set('status', 1);
  }
});

I don't this how this could be related, but...

MY SOLUTION: In fact, I had just missed the part where you learn that onDisconnect is only triggered once. To get a persistent onDisconnect, you need to implement basic persistence.

Helpers.onConnected = function(callback) {
    var connectedRef = lm.newFirebase('.info/connected');
    var fn =  connectedRef.on('value', function(snap) {
      if (snap.val() === true) {
          if (callback) callback();
      }
    });
    var returned = {};
    returned.cancel = function() {
        connectedRef.off('value', fn);
    };
    return returned;
};       

Simple use case:

        this._onConnected = lm.helpers.onConnected(function() {
            this.firebase.onDisconnect().update({ 'tu': 0 });
        }.bind(this));

And then to cancel:

        if (this._onConnected) this._onConnected.cancel();
        this.firebase.onDisconnect().cancel();
like image 507
cwehrung Avatar asked Jun 12 '13 15:06

cwehrung


People also ask

Is it safe to store data in firebase?

It is secure, backed up, and freaking easy to use. And they will only ask for money if you start a paid package, your free hacker package is good to go. If you go over the number of concurrent users, it just won't return data for the newest users.

Is firebase a Realtime Database?

The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime. NEW: Cloud Firestore enables you to store, sync and query app data at global scale.

When should I use firebase Realtime Database?

Primarily synchronizing data, with basic querying. If you don't need advanced querying, sorting and transactions, we recommend Realtime Database. Advanced querying, sorting, and transactions. If you need complex interactions with your data, for example in ecommerce apps, we recommend Cloud Firestore.


1 Answers

Expanding on the comment :

This is why we provide the /.info/connected endpoint. 
We recommend re-establishing the disconnect handler every time the connection comes back online

I followed the above and got it fixed:

const userRef = Firebase.database().ref('/users/<user-id>')

Firebase.database()
  .ref('.info/connected')
  .on('value', async snap => {
    if (snap.val() === true) {
      await userRef.onDisconnect().remove();
      await userRef.update({ status : 'online' })
    } else {
      await this.userRef.remove()
    }
});

For reference, my previous code was:

const userRef = Firebase.database().ref('/users/<user-id>')
userRef.onDisconnect().remove();
await userRef.update({ status : 'online' })

The issue with this is that the onDisconnect may not work when you go offline and come back online multiple times

like image 113
FacePalm Avatar answered Oct 17 '22 08:10

FacePalm