Firebase Realtime Database Connection Issue - No Data Fetching

  • SDK Version: 48
  • Platforms: Android/iOS
  • @react-native-firebase/app: v18.4.0
  • react-native: v0.71.8

Description:

I’m encountering an issue with my Expo mobile app where I’m unable to fetch data from Firebase Realtime Database. Before migrating to Expo SDK 48, I had been using the Firebase JavaScript SDK. During the migration, I decided to switch to @react-native-firebase. I’ve successfully configured Firebase in my app without any problems. However, when it comes to accessing the Realtime Database, I’m facing difficulties.

Symptoms:

  • I have set up Firebase Realtime Database correctly, including the database URL in my Firebase configuration.
  • There are no error messages or exceptions thrown in the console, and the app appears to be running without any issues.
  • I have verified that my app has a stable internet connection because Firebase authentication and Firestore are working as expected.
  • My Firebase security rules allow read access to the data I’m trying to retrieve.

Code Snippet:

javascriptCopy code

import { database } from '@react-native-firebase/database';

database()
  .ref('/users/')
  .on(
    'value',
    (snapshot) => {
      console.log('User exists: ', snapshot.exists());
      console.log('User data: ', snapshot.val());
    },
    (err) => {
      console.error('---- pushToken errrr = ', err);
    },
  );

Current Status:

When I run the above code to fetch data from the Realtime Database, none of the consoles is displayed. However, there are no errors or warnings in the console. To investigate further, I checked the connection using the reference to the special “.info/connected” path, and it always returns false.

I’ve attempted various troubleshooting steps, including checking Firebase rules, verifying the database path, ensuring correct Firebase configuration, and downgrading firebase to old versions. Unfortunately, the problem persists.

Additional Note:

It’s worth noting that the app works fine on the first install, but after closing it and reopening it later, the problem shows up.

I would greatly appreciate any insights, suggestions, or guidance from the community to help me resolve this issue.

I faced the same issue and spent days debugging it. After thorough investigation, I discovered the root cause of the problem. It turns out that the Firebase token doesn’t refresh automatically, which led me down a different path to find a solution.

Firebase JavaScript SDK relies on the Firebase configuration, while @react-native-firebase utilizes the GoogleServices files for configuration. This distinction made me think about comparing the API keys used by both configurations.

The solution that worked for me was to ensure that the ‘Token Service API’ was added to the API keys for both Android and iOS. This step ensured that my app could communicate with Firebase Realtime Database correctly.

To add the ‘Token Service API’:

  • Go to your Google Console Console
  • Select your Firebase project.
  • Navigate to the ‘APIs & Services’ in the left sidebar then Credentials.
  • You should find API key for each platform (Android and iOS), make sure the ‘Token Service API’ is added to ‘API restrictions’ section.

After making this adjustment, I found that my app could fetch data from Firebase Realtime Database without any issues. Hopefully, this solution helps others who encounter a similar problem.

2 Likes

Great, that works like a charm, you saved my life. :confetti_ball:
Thank you so much :blush:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.