Skip to content

Instantly share code, notes, and snippets.

@fm-eb
Created November 16, 2018 10:00
Show Gist options
  • Save fm-eb/d629a7e236a0066624696c9561aaf8ab to your computer and use it in GitHub Desktop.
Save fm-eb/d629a7e236a0066624696c9561aaf8ab to your computer and use it in GitHub Desktop.
Firestore keepalive
private FirebaseDatabase firebaseDatabase;
private DatabaseReference connectedRef;
private ValueEventListener connectionStateListener;
public FirebaseHeartbeatConnector(FirebaseApp firebaseApp) {
L.i("FirebaseHeartbeatConnector() called with: firebaseApp = [" + firebaseApp + "]");
firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp);
firebaseDatabase.setLogLevel(Logger.Level.DEBUG);
}
private void doSendFirebaseHeartbeatRequest() {
L.i("doSendFirebaseHeartbeatRequest() called");
connectedRef = firebaseDatabase.getReference();
connectedRef.keepSynced(true);
connectionStateListener = connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Boolean connected = snapshot.getValue(Boolean.class);
if (connected == null) {
changeState(FirebaseConnectionState.error);
return;
}
if (connected) {
changeState(FirebaseConnectionState.connected);
} else {
changeState(FirebaseConnectionState.not_connected);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
L.i("FirebaseConnectionStateListener was cancelled");
}
});
}
@Grimthorr
Copy link

From the related question on Stack Overflow.

You still need to use the .info/connected listener along with calling keepSynced(true) on the entire Realtime Database (using an empty reference). To do that, you need to replace lines 14-15 with:

firebaseDatabase.getReference().keepSynced(true);
connectedRef = firebaseDatabase.getReference(".info/connected");

@fm-eb
Copy link
Author

fm-eb commented Nov 16, 2018

ah okay, I thought that I have to replace the .info/connected listener. I will try this right now, thanks!

@fm-eb
Copy link
Author

fm-eb commented Nov 16, 2018

Nope, I still get the timeout after 60 seconds :(

@fm-eb
Copy link
Author

fm-eb commented Nov 16, 2018

i also tried it by giving a firebaseInstance to get getInstance() method, but to no avail

@Grimthorr
Copy link

I tested this with the below code and it didn't timeout after 60 seconds for me.

FirebaseDatabase.getInstance().getReference().keepSynced(true);
DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        Boolean connected = snapshot.getValue(Boolean.class);

        if (connected == null) return;

        if (connected) Log.d("presence", "connected");
        else Log.d("presence", "disconnected");
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {  }
});

@fm-eb
Copy link
Author

fm-eb commented Nov 19, 2018

That' really strange. I copy/pasted your code and it worked once. Then I added my firebaseapp-instance as paramter for FirebaseDatabase.getInstance() and it didn't work anymore (1 minute timeout). Then I removed the firebaseapp-instance and it also didn't work anymore...
Then I completely uninstalled the app and went back to your code again => does not work anymore....
i have no idea what's going on there and unfortunately I have no time left to play around with it. So I will implement a normal online-check using the CONNECTIVITY_SERVICE.
Thank you for your time and effort trying to figure this out!! I hope the documentation will improve on this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment