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 ?
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.
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.
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.
There are two cases that may happen when the client and server are disconnected:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With