Serving functions and database in firebase at the same time - firebase

When running firebase serve --port 9000 I serve my firebase app but the console message ends with
Ignoring trigger "onJobWrite" because the Realtime Database emulator is not running.
I can run firebase emulators:start --only database to get the emulator up and running manually but it doesn't seem to work to run it separately.
As far as I can see the function I'm running is not the local one but the one in the cloud and that's not what I want as my trigger is local.
What can I do to make the database trigger the database locally?

Related

GOOGLE_APPLICATION_CREDENTIALS for dummy Firebase project

I'm learning to set up Firebase Emulators correctly to work on my projects and I came up with a problem. I can setup the emulators and make them work locally, however, when trying to access firestore it seems to try to access the real Firestore Instance instead of the emulator.
Right now I'm initializing the app like this (in Cloud Functions)
admin.initializeApp();
const db = admin.firestore();
But when I'm running a function I'm getting:
Failed to initialize and load triggers. This shouldn't happen: Failed to read credentials from file GOOGLE_APPLICATION_CREDENTIALS.json: Error: ENOENT: no such file or directory, open 'GOOGLE_APPLICATION_CREDENTIALS.json'
The thing is that if I use the credentials I generated for my project it will work with the real Firestore instance instead of the emulator.
How should I make credentials for my emulated services?
If you are using Firebase Functions emulator as well then Admin SDK will connect to all the running emulators e.g. if only Auth emulator is running then it'll use the emulator and connect to production for other services like Firestore. You can explicitly set the FIRESTORE_EMULATOR_HOST environment variable and Admin SDKs will use the emulator then.
Checkout the documentation for more information.

Firebase Emulator Suite in different consoles

It would be great to run the Database emulator in a console and then, for example, the functions emulator in another console. In this fashion, You could upload your test-data to the Realtime DB and test/debug your function many times without having to reload all the test-data.
I tried to run only the database in a console like this:
firebase emulators:start --only database
But when I do the same for functions, the emulator hub gets deployed on the next port, so the database and the functions won't communicate to each other, i.e. the triggers won't fire at delete/create/etc. events on the DB.
I tried to find a way to tell the emulator to use a particular running hub, but still no luck.
Any help would be much appreciated.

How to run firebase emulators for local development with functions, triggers on remote db?

I'm building a firebase API with cloud functions and want to reach my production Database for local testing.
My problem :
When I launch my two firebase emulator (functions and firestore) with
firebase emulators:start
1- My custom API endpoints run on http://localhost:5001 (this works)
2- My triggers reaches a local dababase on http://localhost:8080 whereas I didn't set FIRESTORE_EMULATOR_HOST=localhost:8080 (I want to reach my production database)
OR
When I launch only my "functions emulator" with
firebase emulators:start --only function
...but my triggers are not reached, probably because of this warning
i functions[userOnCreate]: function ignored because the firestore emulator does not exist or is not running.
On the other side I'm building a reactjs App also with firebase running on localhost:3000
I call my local API from this app with firebase SDK. To reach my emulator, I add the line :
firebase.functions.useFunctionsEmulator('http://localhost:5001');
The local emulators don't work with actual cloud-hosted instances of databases. Everything must be local.
You are always free to file a feature request.
you have to use firebase serve instead for firebase emulators:start

will firebase triggers fire when running locally?

I have a firebase functions project that connects to a firebase database in the cloud. If I write a database trigger in my firebase functions, and I emulate those functions locally, will the trigger fire? I mean, because the database is running on the cloud, does it even know about my locally running trigger?
No, the local emulator doesn't work that way. The only way you can trigger a database function with the emulator is through the command prompt you're provided with.

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

Resources