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

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

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 functions emulator requesting external network resource: computeMetadata

I have the firebase emulator running in a docker container locally for testing. The emulator includes everything I'm using for my app (firestore, auth, functions, storage) so that I can develop and test independently of the production environment.
However, I'm getting these warnings which are making me nervous:
functions: Beginning execution of "myFunction"
⚠ External network resource requested!
- URL: "http://---.---.---.---/computeMetadata/v1/instance"
- Be careful, this may be a production service.
I don't know what that URL is? Does it mean I've misconfigured something somewhere?
I'm also getting these warnings:
⚠ emulators: You are not currently authenticated so some features may not work correctly. Please run firebase login to authenticate the CLI.
⚠ functions: You are not signed in to the Firebase CLI. If you have authorized this machine using gcloud application-default credentials those may be discovered and used to access production services.
⚠ functions: Unable to fetch project Admin SDK configuration, Admin SDK behavior in Cloud Functions emulator may be incorrect.
But I don't think I want to authenticate, right? I don't want to touch anything to do with the live project on production while testing locally. Can I safely ignore these, or is there a good reason to authenticate?
The warnings are indicative that there had some issues while initialization during the setup for emulators .
Make sure that the emulator is installed by the following command: firebase setup:emulators:firestore, for this you can refer Documentation.
Deploy your function in the firebase in order to get recognized. you can refer to the Documentation using firebase deploy --only functions
Also to be sure please check your Firebase json and see if the local host is configured and not the production host,just to be sure.
For further reference you can follow up the stackoverflow thread Docker authentication issueand Firestore emulatorwhere a similar issue has been raised by other users which might be helpful.

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.

Serving functions and database in firebase at the same time

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?

How can I trigger local Firestore Cloud Functions using the emulator?

I've just set up the Emulator per the instructions here:
Dan$ firebase emulators:start --only functions
Starting emulators: ["functions"]
✔ functions: Emulator started at http://localhost:5001
i functions: Watching "/Users/danfein/Desktop/reviews/backend/functions" for Cloud Functions...
I understand there's something running at :5001. My local DB environment is not running locally (as I understand that's not something supported by Firebase) so because my connections are going out to the web, I don't understand how to trigger something to :5001.
How can I, with my running React instance (running on :3000 if relevant), trigger one of the cloud functions currently living in my functions folder.
The functions do currently run and are appropriately triggered in the cloud.
You will want to set up your development environment as described in the documentation. There is a new emulator suite that ties together Cloud Firestore (which actually can be emulated) and Cloud Functions.

Resources