Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing connection with Firebase database after updating to new SDK

After updating my projects Firebase SDK, I noticed my app regularly lose connection with the firebase database. The time it would take to disconnect ranging from a few minutes to just over an hour. Once disconnected, the app would not reconnect until I have either logged out or cleared the apps data.

Also right before I lose connection, an entry in the log states that my auth token has expired:

PersistentConnection: pc_0 - Auth token revoked: expired_token (Auth token is expired.)

FYI, im using Twitter and Facebook for my authentication and did not experience such issues with the previous Firebase SDK.

I created a new project (with simple auth and real time database) to see if the issue persists and it does. I've attached snippets of that new project:

The build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.sample.gideon.test"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.facebook.android:facebook-android-sdk:4.12.0'
    compile 'com.google.firebase:firebase-database:9.0.0'
    compile 'com.google.firebase:firebase-auth:9.0.0'
    compile 'com.android.support:design:23.4.0'
}

apply plugin: 'com.google.gms.google-services'

The authentication activity follows the firebase facebook login guide, which indeed successfully logs the user in and sends them to the MainActivity which then monitors the database connection using the following code:

MainActivity

    DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
    connectedRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            boolean connected = snapshot.getValue(Boolean.class);
            if (connected) {
                System.out.println("connected");
            } else {
                System.out.println("not connected");
            }
        }

        @Override
        public void onCancelled(DatabaseError error) {
            System.err.println("Listener was cancelled");
        }
    });

Would anyone know what is causing the app to lose connection? So far the error has been experienced in 2 different projects with 2 auth providers (twitter and facebook) and only after updating to the new Firebase.

like image 790
Gideon de Brouwer Avatar asked May 26 '16 13:05

Gideon de Brouwer


People also ask

How do I restore my Firebase realtime database?

To restore your Firebase from a backup, first download the file from Cloud Storage to your local disk. This can be done by clicking the filename within the backup activity section or from the Cloud Storage bucket interface. If the file is Gzip compressed, first decompress the file.

Can Firebase get hacked?

Short Answer : Yes, But it will be hard than a website.

Is there an alternative to Firebase?

NHost is another open-source option for Firebase. The platform works with an amalgamation of open-source technologies, including GraphQL, Postgres, and Hasura. NHost is a serverless backend and helps you build web and mobile applications.

Is Firebase online or offline?

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.


1 Answers

Firebase fixed the connection issue with the release of 9.0.2. Also for anyone still having trouble, I found the answer on this page very useful. Especially the troubleshoot guide produced by the firebase team for those still experiencing auth token issues after updating to 9.0.2.

like image 62
Gideon de Brouwer Avatar answered Nov 01 '22 21:11

Gideon de Brouwer