Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase onDisconnect when turning WIFI off

Tags:

firebase

I noticed when I set up an onDisconnect(), hit my application on a different computer, and turn the WIFI off, my db is not updated; however, it is updated when I turn the WIFI back on. This worries me because I am building an application with expected mobile users and I want to gracefully handle temporary connection drops.

On the otherhand, /.info/connected knows about the disconnection and connection immediately.

Can anyone explain why this is happening and if there is a way to prevent the disconnect from happening once connection is reestablished?

Updated code:

      var connectedRef, userRef;
      connectedRef = new Firebase('https://{fb}/.info/connected');
      userRef = new Firebase('https://{fb}/users/myUser');

      connectedRef.on('value', function (snap) {

        if (snap.val()) {
          userRef.update({ online: true });
          userRef.onDisconnect().update({ online: false }, function () {
            console.log('Turn the Wi-Fi off after seeing this log.');
          });
        }

      });

Result: The db does not set online to false when I turn the Wi-Fi off, unless I wait about 1 minute. The db does set online to false when I turn the Wi-Fi back on.

like image 513
jwzirilli Avatar asked Jul 15 '14 20:07

jwzirilli


1 Answers

Turning off your wifi does not close the sockets in an efficient manner. Thus, the server has to wait for the socket to time out before it can fire onDisconnect. Since this is an entirely server-side process, the only possible outcomes are:

1) The user isn't allowed to perform the onDisconnect op (indicated in the callback immediately upon establishing the onDisconnect)

2) The event will fire when the socket times out or disconnects (the length of time is completely up to the browser/server negotiation (1 minute is not unreasonable)

3) Some data changes in Firebase between the time of establishing onDisconnect and the event firing that makes it invalid (the security rules won't allow it because the op is no longer valid)

To see your onDisconnect() fire a bit faster, try using goOffline(), which I believe will properly close the socket connections.

like image 106
Kato Avatar answered Sep 17 '22 20:09

Kato