How do Firebase functions handle PubSub topics created with Google Cloud? - firebase

I found a two sources that indirectly reference the fact that when creating Google Cloud PubSub subscriptions with Firebase (using the firebase-functions/v1 npm package), Firebase automatically creates the PubSub Topics and PubSub subscriptions:
Cap your Firebase spending #1: Setting up a billing PubSub topic (YouTube)
Firebase PubSub Trigger with Message Ordering (StackOverflow)
As a result, you do not need to (for example) use $ gcloud pubsub topics create $TOPIC_NAME and $ gcloud pubsub subscriptions create $SUBSCRIPTION_NAME to create the topic and subscriptions.
Is this behavior actually documented anywhere?
I would like to create my PubSub subscriptions outside of Firebase. This enables me to create subscriptions using the "dead letter topic" feature and enables me to consistently document my Topics and Subscriptions within my larger Google Cloud Project.
Is it possible to deploy cloud functions within my Firebase project that listen to subscriptions created outside of Firebase?
For example, could I deploy Firebase cloud functions that use the #google-cloud/pubsub npm package instead of firebase-functions/v1?

Firebase automatically creates the PubSub Topics and PubSub subscriptions as you can see that in the process & code described in public docs.
And It is possible to deploy cloud functions within Firebase projects that listen to subscriptions created outside of Firebase. If you want to deploy cloud function which handles dead-letter-topics in firebase you have to follow these steps:
Create a pubsub topic using gcloud command "gcloud pubsub topics
create"
Create a firebase function with trigger pointing to the
topic created in step 1
Deploy the function using firebase
command "firebase deploy --only functions"
Update the subscription
created and enable dead letter feature. This could also be done
using the gcloud command
Make necessary updates to the firebase
function and redeploy the function using firebase deploy --only functions
Reason behind updating subscription is, When deploying the function using the firebase cli, the subscription may be recreated with standard retry settings and results in disabling dead-letter-topic.
So After each function deployment, you need to update the subscription accordingly.
You can use postdeploy hooks in firebase cli through which you can re-enable the dead-letter settings post a deployment automatically. Please see the below steps for the same:
Create a postdeploy.sh script that contains the gcloud command to update a subscription as below. Please see below as just an example and should be modified for variables. Command details are available here
gcloud pubsub subscriptions update projects/$GCLOUD_PROJECT/subscriptions/$SUBSCRIPTION_NAME --dead-letter-topic=$TOPICNAME --max-delivery-attempts=5
Reference the above postdeploy.sh in firebase.json file as a post hook. Example below:
{
"functions": {
"postdeploy": "./postdeploy.sh",
"source": "functions"
}
}
Deploy the firebase function using regular firebase command firebase deploy --only functions:XYZ
For other alternatives go through this Article.
And you can deploy firebase functions which uses the #google-cloud/pubsub

Related

What causes typescript compile error when deploying

I roughly following this tutorial to make a cloud function with typescript. It works fine in development, but when I try to deploy it (firebase deploy --only functions), I get A LOT of typescript errors. I have no idea why. It works in dev, why can't I push the same set up to Firebase?
Follow the below steps for a successful cloud functions deployment:
After you have created a Firebase project using this step, install
the Firebase CLI using : npm install -g firebase-tools.
Run firebase login to log in via the browser and authenticate the
firebase tool.
Run firebase init firestore
Run firebase init functions
When you run firebase init functions, there will be prompts, 1st
prompt : “What language would you like to use to write Cloud
Functions?” -> Choose Typescript. 2nd prompt : Do you want to use
ESlint to catch probable bugs and enforce style (y/n)? -> Choose n.
By selecting no you will be saving yourself from the typescript
compile errors.
Once initialization is complete, uncomment the sample in index.ts and
run npm run serve to see a "Hello World" function in action. Once you
can see the output in Firebase emulator, you will know the functions
executed properly.
Now, before you deploy the functions, assign the Cloud Functions
Invoker, Cloud Functions Admin and Cloud Functions Developer role to
project-id#appspot.gserviceaccount.com
Now run firebase deploy –only functions. The deployment will be
successful but the browser might show 403 errors ( Client does not
have permission..) For that, select your cloud function (check box)
click "Add members" under the Permissions tab in the right side,
enter "allUsers" under "New members", select Role as Cloud Functions -> Cloud Functions Invoker and then save the configuration.
If you do not want to give public access to your cloud functions, you
can set up authentication, by skipping the previous step. In Cloud
Shell, run gcloud auth print-identity-token. This outputs a token
which you will be authenticating your cloud function with. Now
provide authentication credentials in your request as a
Google-generated ID token stored in an Authorization header with this
command :
curl https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME \ -H "Authorization: bearer $(gcloud auth print-identity-token)"

Firebase CLI is not recognizing my project

Why can't anything be easy? I am trying to add push notifications from Firebase Cloud Messaging. When I run firebase init I get the following:
How do I get this to work? I have tried all the solutions I have read here but none of them have worked.
The CLI is recognizing your project, but the initialization is failing due to an error with Cloud Storage for Firebase.
Try going to the Storage section within your Firebase Console (here) and initialize Cloud Storage for Firebase first.
After you've chosen a location for your bucket try running your command again.

How to Firebase cloud function deploy privately

When i use firebase deploy --only functions to deploy cloud functions for firebase, i discover, that this functions are deployed with the authentication flag allUsers.
How can i deploy firebase cloud function with private by default as mentioned here ?
There is no way to set this access control level of Cloud Functions through the Firebase CLI. It currently indeed always sets the access to allow all users. It sounds like a reasonable request to allow control of this though, so I'd file a feature request and possibly a PR on the repo.
For now: if you want to set this access level, you will have to do so in the Cloud console as explained in the Google Cloud documentation on controlling access on a function.

Firebase trigger function ignored due to missing "firebaseauth.googleapis.com emulator"

I have a Firebase function createUser that is triggered by auth.user().onCreate.
Whenever I serve functions locally, it is ignored.
I get the same output when I run firebase serve, firebase serve -only functions,firestore, firebase emulators:start, or firebase emulators:start --only functions,firestore:
⚠ functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /home/david/Sync/alikely/key.json. Non-emulated services will access production using these credentials. Be careful!
✔ functions[createItem]: http function initialized (http://localhost:5000/alikely-ce1d5/us-central1/createItem).
i functions[createUser]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.
As shown above, I have attempted to follow the instructions to setup admin credentials here: https://firebase.google.com/docs/functions/local-emulator.
I have initialized functions and firestore with firebase init, and my project directory contains a firestore.rules, firestore.indexes.json, and a firebase.json. Are the contents of one of these files to blame?
How can I locally emulate all of my functions for full local development? How can I enable the "firebaseauth.googleapis.com emulator"?
The documentation you linked says:
The Firebase CLI includes a Cloud Functions emulator which can emulate
the following function types:
HTTPS functions
Callable functions
Cloud Firestore functions
Auth functions are currently not supported, just the three types mentioned above.

Unable to trigger firestore changes locally

I just started doing firebase and I need to do something when a new document is created but in functions and I setup cli and started locally but the function is not triggered.
while doing firebase init I chose firestore,functions,storage
after that did firebase login and firebase serve --only functions
and the server is up but whenever I add new document the function is not getting triggered?
You should be able to do that with firebase emulators:start. This starts emulators for Cloud Functions, Cloud Firestore, Realtime Database and Firebase Hosting. If that doesn't work, please share your code and I can have a look.
The docs may be useful too: https://firebase.google.com/docs/functions/local-emulator#run_the_emulator_suite

Resources