Access firebase emulator from local network

Viewed 8170

I'm using firebase emulators:start firebase command to emulate functions locally. This work fine on iOS simulator but if I use real android device which is in the same network, these functions are unreachable. I can access 5001 port like this: locahost:5001 but not like this: 192.168.x.x:5001.

I have the following code in my react-native expo project:

export const functions = firebase.functions();
firebase.functions().useFunctionsEmulator('http://192.168.x.x:5001');

but again, this only works on a simulator if I change the last line to:

firebase.functions().useFunctionsEmulator('http://localhost:5001');

Is it possible to start the emulator with something like --host option like in firebase serve command? Is there any other solution?

3 Answers

Set your firebase.json to

"emulators": {
    "functions": {
      "port": 5001,
      "host": "0.0.0.0"
    },
    "firestore": {
      "port": 8080
    },
    "database": {
      "port": 9000,
      "host": "0.0.0.0"
    },
    "hosting": {
      "port": 5000
    }
  }

The "0.0.0.0" host tells your computer to use localhost and your local ip for your network at the same time

Check Android FirebaseDatabase library changelog (version 19.1.0 explains this new feature) https://raw.githubusercontent.com/firebase/firebase-android-sdk/6ae83de756628b933c7ddaf1153c14af0315c945/firebase-database/CHANGELOG.md

To listen on a different host than the default: localhost just check the --help

$ firebase serve --help
Usage: serve [options]

start a local server for your static assets

Options:

-p, --port <port>   the port on which to listen (default: 5000) (default: 5000)
-o, --host <host>   the host on which to listen (default: localhost) (default: localhost)
--only <targets>    only serve specified targets (valid targets are: functions, hosting)
--except <targets>  serve all except specified targets (valid targets are: functions, hosting)
-h, --help          output usage information

In this case:

firebase.functions().useFunctionsEmulator('http://0.0.0.0:5001');

or

firebase serve -o 0.0.0.0

Because 0.0.0.0 means Current network (only valid as source address).

Related