Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Firebase myConnectionsRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/connections");

// stores the timestamp of my last disconnect (the last time I was seen online)
final Firebase lastOnlineRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/lastOnline");

final Firebase connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      // add this device to my connections list
      // this value could contain info about the device or a timestamp too
      Firebase con = myConnectionsRef.push();
      con.setValue(Boolean.TRUE);

      // when this device disconnects, remove it
      con.onDisconnect().removeValue();

      // when I disconnect, update the last time I was seen online
      lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
    }
  }

  @Override
  public void onCancelled(FirebaseError error) {
    System.err.println("Listener was cancelled at .info/connected");
  }
});

The trouble is I don't know when onDisconnect event would be fired?!

Every time I open the app I write TRUE to the node "users/joe/connections" but when I closed app nothing would be happend. When I switched WIFI off then the boolean parameter wouldn't be removed as well. onDisconnect-event only fired when I forcely stopped my app or reinstalled this app or somewhen else when I couldn`t determine.

So, as I understand right I have to handle manually such events:

1) close my app;

2) switch WiFi off;

3) maybe something else

for creating Presence feature in my app? But then when have onDisconnect-event to be fired ?

like image 301
Alex Zezekalo Avatar asked Jun 05 '15 13:06

Alex Zezekalo


People also ask

When should you not use firebase?

With Firebase, you can't deal easily with data-migration like you can do with a simple SQL database. Firebase uses JSON and there are almost no features SQL features, so you wouldn't be able to migrate from the existing database easily. If you're managing large amounts of highly structured data.

How much traffic can firebase handle?

While there isn't a limit to how many read or write operations you can trigger from a single function, a single database write operation can only trigger 1000 functions, or 500 functions per region for Cloud Functions v2.

Is firebase storage realtime?

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing: Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model.


1 Answers

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.

like image 198
Frank van Puffelen Avatar answered Oct 22 '22 12:10

Frank van Puffelen