33

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?

8
  • Possible duplicate of firebase cli serve cant access the project from different device
    – eapo
    Commented Nov 27, 2019 at 1:41
  • 2
    I don't think this is duplicate. firebase serve is not the same as firebase emulators:start. Commented Nov 27, 2019 at 12:39
  • I still can't see any difference between the two questions focusing on the main goal and there is a good answer to.
    – eapo
    Commented Nov 27, 2019 at 22:01
  • 2
    @KingJulien Did you every figure out a solution to this?
    – Conner
    Commented Mar 21, 2020 at 21:56
  • 2
    @KingJulien Actually I figured it out yesterday haha... set "emulators": { "functions": { "port": 5001, "host": "0.0.0.0" }, "firestore": { "port": 8080 }, "database": { "port": 9000, "host": "0.0.0.0" }, "hosting": { "port": 5000 } }
    – Conner
    Commented Mar 28, 2020 at 20:00

3 Answers 3

76

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

2
8

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).

2
  • The cause of the firebase serve in my answer: "Is it possible to start the emulator with something like --host option like in firebase serve command?"
    – eapo
    Commented Nov 27, 2019 at 21:53
  • 1
    firebase serve is now deprecated; see stackoverflow.com/questions/56618725/…
    – Nick
    Commented Jul 22, 2021 at 9:01
1

To build on Conner's answer - if you're testing on a physical device, you'll have a better time connecting using your computer's IP address. Rather than hardcoding it, you can use a built-in variable: Constants.manifest.debuggerHost?.split(":").shift().

Wrote up a short explanation on that here:

https://dev.to/haydenbleasel/using-the-firebase-local-emulator-with-expo-s-managed-workflow-5g5k

Not the answer you're looking for? Browse other questions tagged or ask your own question.