30

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
King Julien
  • 10,981
  • 24
  • 94
  • 132
  • Possible duplicate of [firebase cli serve cant access the project from different device](https://stackoverflow.com/questions/48769168/firebase-cli-serve-cant-access-the-project-from-different-device) – eapo Nov 27 '19 at 01:41
  • 2
    I don't think this is duplicate. `firebase serve` is not the same as firebase `emulators:start`. – King Julien Nov 27 '19 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 Nov 27 '19 at 22:01
  • That answer does not apply to my question. There is no `-o` option in `firebase emulators:start` – King Julien Nov 28 '19 at 06:34
  • 2
    @KingJulien Did you every figure out a solution to this? – Conner Mar 21 '20 at 21:56
  • @Conner Nope, unfortunately they do not support this feature yet. – King Julien Mar 28 '20 at 10:40
  • 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 Mar 28 '20 at 20:00
  • I can't even make it work for iOS simulator, which post do you use for simulator to connect? – xmanreturn Nov 06 '22 at 01:53

3 Answers3

71

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

Conner
  • 1,771
  • 11
  • 24
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).

eapo
  • 1,053
  • 1
  • 19
  • 40
  • 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 Nov 27 '19 at 21:53
  • 1
    `firebase serve` is now deprecated; see https://stackoverflow.com/questions/56618725/firebase-serve-only-functions-vs-local-emulator-to-run-cloud-functions-locally – Nick Jul 22 '21 at 09: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

Dharman
  • 30,962
  • 25
  • 85
  • 135