Run Firebase Callable HTTP Cloud Function locally
I suspect your host and port values might be incorrect.
When you start up your emulator, the logs will display all the ports that are being used for your various emulators. There's also the firebase.json
file that has the port configurations for the emulators.
Also, localhost
is not automatically identified by the clients. For Android you might have to configure networkSecurityConfig
in your manifest by creating a network security config xml. For iOS, I think there should be something that needs to be added in the plist
file - has to be checked confirmed.
Once your clients are able to access localhost
(implicitly or explicitly), you can connect to your emulated functions. For example, I have explicitly state the host value in my code (this is not expected to change across restarts).
Here is the code that I use in Kotlin:
functions = FirebaseFunctions.getInstance()
/*
* Emulator
* */
if (emulatorMode) { //Local Testing
functions.useEmulator("10.0.2.2", 5001)
} else { //Live
functions = Firebase.functions
}
// Call the function and extract the operation from the result
return functions
.getHttpsCallable(function)
.call(inputData)
.continueWith { task ->...
iDecode
When developers want to hide something from you, they put it in docs.
Updated on December 10, 2022Comments
-
iDecode over 1 year
I've a deployed a callable function, say
foo
, usingfirebase deploy --only functions:foo
and I'm calling it in Flutter using:
var result = await FirebaseFunctions.instance.httpsCallable('foo')(); var data = result.data;
All good. But I'd like to run this function in the Firebase Emulator. When I run
firebase emulators:start --only functions
I can see the emulator up and running, and I'm connecting my Flutter app to the emulator using:
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 4000);
However, I don't know how to invoke the callable
foo
in Flutter using the Firebase Emulator. I saw this related thread but it doesn't really answer my question.
EDIT:
Here's the console output:
✔ functions[us-central1-foo]: http function initialized (http://localhost:5001/my_project/us-central1/foo). ┌─────────────────────────────────────────────────────────────┐ │ ✔ 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 │ ├───────────┼────────────────┼─────────────────────────────────┤ │ Firestore │ localhost:8080 │ http://localhost:4000/firestore │ └───────────┴────────────────┴─────────────────────────────────┘ Emulator Hub running at localhost:4400 Other reserved ports: 4500
I tried
useFunctionsEmulator('localhost', 4000); useFunctionsEmulator('localhost', 5001);
but both of them throw the following error on calling
httpsCallable('foo')
but without the emulators, this function does work.[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_functions/unavailable] UNAVAILABLE
-
Mattia Galati over 2 yearsHave you already checked here: firebase.flutter.dev/docs/functions/usage#emulator-usage?
-
iDecode over 2 years@MattiaGalati Yes, I checked that out already but there was no helpful code in the link.
-
Mattia Galati over 2 yearsIf you run the
FirebaseFunctions.instance.httpsCallable('foo')
after theFirebaseFunctions.instance.useFunctionsEmulator
setting, won't the function on the emulator being called? Does you emulator function replies if you call it, for example, with Postman or similar tool? -
iDecode over 2 years@MattiaGalati Please check out the edited post.
-
Mattia Galati over 2 yearsAs per you edit, I see the cloud function should be available on localhost:5001. Does calling it from a PostMan-like tool (or curl) on the same pc results in a correct reply? If so, and you are testing the app on a phisical device, maybe the Dart code should be modified to use the IP of the pc where the emulator is running in the local network.
-
iDecode over 2 years@MattiaGalati I don't have the PostMan installed on my computer. I'm testing it on a physical device though. Can you please tell me how can I use the IP of the pc in this case? Thanks
-
Mattia Galati over 2 yearsthis code might run but depends on what platform you are:
curl --request [GET/POST] 'http://localhost:5001/[project]/[region if used]/[function]' --header 'Content-Type: application/json' --data-raw '{"data": {"foo": "bar"}}'
If the request reaches the local function, the run aipconfig
orifconfig
and search for your local lan IP, and on your dart code useuseFunctionsEmulator('[LOCAL_IP]', 5001);
-
-
srinij over 2 yearsAre you using a physical device or a device emulator? I think a physical device cannot connect to localhost on your computer and only device emulators can.
localhost:5001
is the right one to use in your code to connect to functions. -
iDecode over 2 yearsI'm using a physical device. Can I never be able to test the app on my device in that case?
-
srinij over 2 yearsYou should be able to by configuring the IP address of your machine as the host. I found this [stackoverflow.com/questions/66161387/….
-
iDecode over 2 yearsThanks, although I'm unable to find the answer but I'll surely give you an upvote for all your help later :)
-
iDecode over 2 yearsUnhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 45398. Can you post a full solution?
-
iDecode over 2 yearsI updated my
firebase.json
file to use"host": "0.0.0.0"
for functions emulator, did a fresh restart, and useduseFunctionsEmulator('my_ip_address', 5001);
but I'm still getting the same error I mentioned in my edited post. -
iDecode over 2 yearsIncomplete answer. What should I type after
ipconfig
, this is its signature:ipconfig <command> <args>
-
iDecode over 2 yearsSecond, there is no
origin
named parameter. -
Rafael Zasas over 2 yearsSidenote Don't be afraid to use the V9 SDK as you can still use the
compat
library that they provide to upgrade specific solutions one at a time. -
iDecode over 2 yearsThanks but I wanted to test that on a real device.
-
Rafael Zasas over 2 years@iDecode I see. You can still run the shell emulator however and run the functions locally which will at least allow you to test them outside of the app environment. As for testing on the device it may be useful to convert the function to http on request. I added the Firebase docs instructions for Flutter in case anyone strolls by looking for more info.
-
iDecode over 2 yearsIn case you missed it, I am already using
useFunctionsEmulator
and that too isn't working which is why the question was asked.