2

I am trying to connect to my Firebase functions emulators from my test Android device.

When I run the emulators the output is:

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4000/functions │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.

From my Flutter app, I do the following to connect:

FirebaseFunctions.instance.useFunctionsEmulator(origin: 'http://192.168.1.158:5001');

I have added android:usesCleartextTraffic="true" to my AndroidManifest and a network_security_config.xml as stated here and here.

I am getting all the time the following error:

PlatformException(firebase_functions, com.google.firebase.functions.FirebaseFunctionsException: INTERNAL, {code: unavailable, message: com.google.firebase.functions.FirebaseFunctionsException: INTERNAL}

What am I doing wrong?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
svprdga
  • 2,171
  • 1
  • 28
  • 51

1 Answers1

5

By default, all the emulators will listen to localhost only and not your local network.

I tried replicating your issue by running my hosting emulator on the whole network and functions emulator only on localhost as in the following screenshot.

enter image description here

const firebaseConfig = {...}
firebase.initializeApp(firebaseConfig);

firebase.functions().useEmulator("192.168.0.102", 5001);

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: "messageText" }).then((result) => {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;
}); 

I copied the sample function from the documentation and tried calling the function and as expected:

enter image description here

If you serve the functions using firebase serve --only functions --host 0.0.0.0 it should make functions available for your network.

Alternatively, you can specify that in your firebase.json like this:

{
  "functions": {
    "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
  },
  "emulators": {
    "functions": {
      "port": 5001,
      "host": "0.0.0.0"
    }
  }
}

Then you can simply start the emulators using firebase emulators:start.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • 1
    YES.. I LIKE THAT YOU MENTIONED: Then you can simply start the emulators using firebase emulators:start. I was just changing the host but not restarting the emulators!! – i_o Feb 27 '22 at 20:59