Kid
Kid

Reputation: 1240

React-Native-Firebase : RTDB Emulator

Does anybody knows how to use realtime database emulators?

I can use function & firestore emulators like below.

import functions from '@react-native-firebase/functions';
import firestore from '@react-native-firebase/firestore';

functions().useFunctionsEmulator('http://localhost:5001');
firestore().settings({ host: 'localhost:8080' });

But was not able to find something similar for realtime database. Any link/video is appreciated.

Thanks.

Upvotes: 2

Views: 1727

Answers (3)

Nick Young
Nick Young

Reputation: 895

I have this in my root index.js file for my react native project:

// Use a local emulator in development
if (__DEV__) {
  // If you are running on a physical device, replace http://localhost with the local ip of your PC. (http://192.168.x.x)
  auth().useEmulator('http://localhost:9099');
  functions().useFunctionsEmulator('http://localhost:5001');
  database().useEmulator('localhost', 9000);

  const db = firestore();
  db.settings({ host: 'localhost:8080', ssl: false });
}

Nothing else in my app needed to be modified. The database().useEmulator line does the trick.

I am assuming you have already initialized all the emulators first using firebase init emulators and have them running with firebase emulators:start.

Make sure you're using the ${projectId}-default-rtdb database in the emulator.

Upvotes: 1

KMW
KMW

Reputation: 121

Basically the solution posted by Frank van Puffelen was correct. However, this got me stuck for a while so I'm going to share my experience to save time for others.

import { firebase } from "@react-native-firebase/database";
const database = firebase
  .app()
  .database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");
const ref = database.ref(`yourPath`)
...

Here comes the huge gotcha: If you're using Android Emulator you have to use

const database = firebase
  .app()
  .database("http://10.0.2.2:9000?ns=YOUR_DATABASE_NAMESPACE");

If you're using a real android device you have to set up reverse proxy first:

adb reverse tcp:9000 tcp:9000

then set up the database normally

const database = firebase
  .app()
  .database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");

I didn't test for iOS, but I believe localhost should work.

Upvotes: 4

Frank van Puffelen
Frank van Puffelen

Reputation: 599716

According to the documentation on Connect your app to the Realtime Database Emulator, that is done with:

if (location.hostname === "localhost") {

  var firebaseConfig = {
    // Point to the RTDB emulator running on localhost.
    // In almost all cases the ns (namespace) is your project ID.
    databaseURL: "http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE"
  }

  var myApp = firebase.initializeApp(firebaseConfig);
  var db = myApp.database();
} 

I'm not certain if that is supported in react-native-firebase yet, since it isn't mentioned in their documentation at all. The most logical things to try would be their Using a secondary database:

const database = firebase.app().database('http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE');

Upvotes: 0

Related Questions