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
Related
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
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.
I am having trouble running the firebase functions emulator towards a production database. I have a project which is not publicly released yet so I can run towards production with any negative effects.
My project uses only the realtime database, it does not use Firestore (so other questions on SO are not relevant) The documentation states "Cloud Firestore and Realtime Database triggers already have sufficient credentials, and do not require additional setup." so according to that, I shouldn't need any additional setup in order to point to the production database.
According to all of the documentation on Firebase, the project should run towards the real firebase database if I only start the functions emulator and do not start the database emulator. This warning seems to say so, too:
functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, hosting, pubsub
However, this is not what happens. Instead, I get the following error:
functions[onGlobalClientRequest]: function ignored because the database emulator does not exist or is not running.
I have read the firebase documentation and nothing is really mentioned other than setting credentials should not be needed (but I am setting credentials anyway using export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json" before running the functions emulator.)
onGlobalClientRequest looks like this:
export const onGlobalClientRequest = functions.database
.ref( client_requests_key + "/{pushedid}")
.onCreate(
async (
snap: functions.database.DataSnapshot,
context: functions.EventContext,
) => {
///.... code here...
},
);
The locally emulated Firebase Functions will be able to write to the Prod database, but will not be able to get triggered by the production database.
Here is a SO answer from a Google employee that states the aforementioned.
Also, quoting from this other SO answer from another Google employee:
In general the rule with emulators:start is that we comprehensively emulate whatever is running. So for example if you're running the Functions and Database emulator, all writes from functions (through admin.database().... will be redirected to the Database emulator. But writes to Firestore (admin.firestore()...) will try and hit production because that emulator is not running.
Operations on a Realtime Database (RTDB) trigger the functions in the corresponding project. So the production RTDB triggers functions of your production project, and the emulated RTDB triggers emulated functions.
If you want to test functions triggered by RTDB operations locally with the emulator, you have to use the RTDB emulator as well. This is explained in the doc here.
If you only want to test HTTP callable functions, then you can use a remote RTDB.
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.
I know that I could setup Cloud Firestore and GCP Cloud Functions with infrastructure as a code, but I'm interested that it will be shown at the Firebase Console. I also can't find any way to deploy Firebase Auth and Realtime database with IaaC.
Any Cloud Functions deployed by either the Firebase CLI or gcloud will appear in both the Firebase console and the Cloud console.
Any data populated in Firestore will also appear in both consoles.
The Cloud console has no view into Firebase Auth or Realtime Database, as those services are unique to Firebase. You will have to use the Firebase console and its tools and SDKs to work with those products.
In fact, a Firebase project is just a Cloud project with extra APIs and services enabled on top. You might be helped by reading this blog series on the relationship between Firebase and Google Cloud.
https://medium.com/google-developers/whats-the-relationship-between-firebase-and-google-cloud-57e268a7ff6f
https://medium.com/google-developers/firebase-google-cloud-whats-different-with-cloud-functions-612d9e1e89cb
https://medium.com/google-developers/firebase-google-cloud-whats-different-with-cloud-firestore-40f1fc3e6d1e
https://medium.com/google-developers/firebase-google-cloud-whats-different-with-cloud-storage-a33fad7c2b80