Firebase: when onDisconnect event fires?

I am using Firebase-backend for my android application. I would like to create a user presence system for my chat. For this, I took a template from the 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 problem is that I donโ€™t know when the onDisconnect event will be fired ?!

Each time I open the application, I write TRUE for the node " users / joe / connections ", but when I closed the application, nothing worked. When I turned off WIFI, the boolean parameter will also not be deleted. onDisconnect-event is triggered only when I stopped my application or reinstalled this application, or sometime else, when I could not determine.

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

1) close my application;

2) disable Wi-Fi;

3) maybe something else

to create a presence function in my application? But then when do you have onDisconnect-event ?

+8
android firebase firebase-database
source share
2 answers

There are two cases when the client and server are disconnected:

  • the client is clearly disconnected from the server
  • the client just disappears

When you kill the application, you cause an explicit shutdown. In this case, the client sends a signal to the server to disconnect it, and the server immediately executes the onDisconnect .

When you turn off Wi-Fi, your application will not be able to tell the server that it is disconnecting. In this case, onDisconnect triggered when the server detects that the client has left. This may take several minutes, depending on how socket timeouts are configured. Therefore, in this case, you just need to be a little more patient.

+13
source share

When the user exits the application, you can call admin.database().goOffline() to explicitly disconnect from firebase, which should cause the onDisconnect . When the user returns to the application, just call admin.database().goOnline() to reconnect to firebase.

0
source share

All Articles