Firebase: how to debug onCall functions? - firebase

I am using Google Cloud Functions Emulator for debugging locally my Firebase functions.
Firebase recommend to use functions.https.onCall functions instead of functions.https.onRequest for avoid using http calls directly. Instead Firebase recommend to use Firebase Functions SDK for call such functions from code.
For functions.https.onRequest I used http-trigger flag but how can I debug onCall functions ?

I found a way to run 'onCall' cloud functions locally and call them from a local client. I still haven't figured out how to attach a debugger but at least I can iterate faster and use console.log statements.
Use firebase serve to host the functions. Even though these are supposed to be for http requests I found that the the firebase clients have a property to override the host name for onCall functions. This takes care of authentication and passing in the right data and context objects for you.
From Terminal in the root of your firebase project folder:
firebase serve
You should get a successful result in the terminal window. Take the hostname and port number:
✔ functions: helloWorld: http://localhost:5000/your-project-name/us-central1/helloWorld
iOS client:
Functions.functions().useFunctionsEmulator(origin: "http://localhost:5000")
NOTE: you have to disable App Transport Security in iOS for this to work
NOTE: other clients probably have the same API.

Related

Firebase emulators blocking firebase functions?

So in my web app, I am calling an https callable cloud function like so
const createStripeCheckout = functions.httpsCallable('createStripeCheckout')
//Call function
And up until I started using the firebase emulator, everything was fine. It was making a request to the https endpoint, and the checkout was made successfully. But after I used functions in the emulator, it makes a request to the local dev server (localhost:5000/firebase-project/name) instead of the correct https endpoint hosted by firebase (firebase/name) and the function can't work unless I start the emulator, and will only work on my computer. I don't recall changing any settings.
Found the problem, in my code there was a line that read:
functions.useEmulators("localhost", 5001)
I guess that directed all functions to that adress

Can I deploy a Firebase https callable (.onCall) using Terraform?

So AFAIK, all triggered cloud functions can essentially be deployed using google deploy command which in turn allows me to deploy through terraform through similar parametrization. That being said, I can't seem to deploy a https callable function with:
event_trigger {
event_type = "https.onCall"
resource = ""
}
My assumption is that it's because https callables are explicitly a firebase only "system".
Has anyone gone through this kind of situation before?
Firebase's callable functions are just HTTPS functions that follow a special protocol. The function-side wrapper around that is provided by the firebase-functions SDK and deployed only by the Firebase CLI. If you can't use the Firebase CLI, you're going to have a hard time coming up with an equivalent way to deploy that code. It might be easier to just code your own protocol on both the client and function and deploy with the tools that are compatible with your deployment process.

Is it possible for Firebase Cloud Functions to receive http request from an extenal server, while on Blaze Plan?

I have been searching and haven't found whether it is possible for a firebase project using the Blaze Plan to receive http requests from an external server.
If you have implemented https functions, you can recieve https requests from external server, after you deploy the function you'll get url for each functions either from terminal or firebase console in functions section, check the github repo for sample

Prevent firestore triggers when serving firebase functions locally

I am new to Firebase, I setup a project and using nodejs + nestjs
I have a https trigger function that adds record to Firestore
I have a Firestore document onCreate trigger, to duplicate data to another collection
Then I try to run Firebase locally by running npm run serve which is calling firebase serve --only functions
Then I can access the https function via postman app.
First problem is Firestore onCreate trigger is not fired locally when I call my local https function.
Second problem is Firestore onCreate trigger is run on server and I can see running logs, which means in development time, some buggy bad code could be running on server and that code might corrupt data (while the good code is under development and bugs are fixing on my local)
So my question is, how usually people do development on their local and testing?
firebase serve --only functions only emulates HTTPS functions.
firebase experimental:functions:shell works with everything else, but you have to construct stub data to send to it.
See the documentation for more information:
Run Functions Locally
Unit testing functions

update Firebase emulator function timeout duration

When emulating firebase functions locally is there a way to change the function timeout from the default 60s?
--idlePruneInterval has been added as an option to the google cloud emulator CLI: https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/66
This option doesn't appear to work is not officially supported in either the firebase serve mode:
firebase serve --only functions --idlePruneInterval=5000
or the experimental shell emulation:
firebase experimental:functions:shell --idlePruneInterval=5000
Is there another way around this problem? Perhaps editing a config file?
The Cloud Functions emulator from Firebase is separate from the one provided by the Google Cloud Platform org. The command line parameters from one won't necessarily work on the other.

Resources