Firebase cloud functions emulators always write to a default database - firebase

I recently set up the firebase emulators to run my cloud functions locally. After setup, the cloud functions triggered successfully but any write to the real-time database does not reflect in the corresponding local database emulator UI. e.g when I use
snapshot.after.ref.parent.child('busy').set(true)
So I tried exporting and importing the real-time data and I discovered that all database write from cloud functions was being saved in a default database with name localhost:4000/?ns=pick2-c468b-default-rtdb and not the database that is triggering the write event.
Is this the default behaviour of the emulator and how do I go about changing or fixing it?

Writes to snapshot.after.ref should go to the database that triggered the Function.
If that is not happening for you, I recommend reporting a bug with a minimal repro on the repo (which should include an entire Cloud Function, and not just the line that you think is going wrong.

Related

Prevent wiping data after stopping firebase simulation on localhost

tanx to google , firebase can deploy on localhost for testing. afer stoping simulation all of the states are going to reset and database is wiping out too.
I need to know how to avoid from losing this data and configs.
If you're using Cloud Firestore you can import data to and export data from the emulator suite. For other emulated products such functionality does not (yet) exist, so you will have to ensure you can re-initialize the data in your application code.

Run firebase functions emulator on database without database emulator (towards production cloud instead)

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.

Firestore cloud functions not triggering without deploying functions

Can I use firestore triggers without first deploying my functions? I am getting an error that looks like it but I want to be sure.
If you want a Firestore trigger to respond to a change in a document that's actually in Firestore (and not running in a local emulator), then yes, you will have to deploy the trigger. Cloud Functions only works when deployed to Google's cloud.

How To Prevent Firebase Firestore Emulator From Clearing The Database At Exit?

I'm Planning To Use Firebase - Firestore Emulators For Handling Some Huge Amount Of Data That's Coming From A Websocket.
I just need to process the data locally in realtime.
Firestore Emulator Is A great choice since it has all the features I need. But the issue is it clears the database every time the Emulator is turned off. is there any config or settings that can change this behaviour?
if it is not possible. can somebody tell me any other Approach to use firestore locally? or any other alternative to firebase that I can use locally.
With the recent updates to Firebase Emulator, you use export the data when the emulator is running and import when it starts again.
To export the data
firebase emulators:export <export-directory>
To start and import the emulator
firebase emulators:start --import <export-directory>
I've managed this starting Firebase emulator with parameters to export data on exit and import existing one:
firebase emulators:start --import=exported-dev-data --export-on-exit=exported-dev-data
This way the emulator always export the data I've generated to folder exported-dev-data, and loads it back when starting again.
Currently you need to write code to populate the database during each run of the emulator.
If you would like to see different behavior, please file an issue on GitHub.

maintain connection status of user with firestore. using firebase db and cloud function

I am trying to maintain connection status of user with firestore . but i don't understand some points.
Link : https://firebase.google.com/docs/firestore/solutions/presence
i refer the above link description but i don't understand how to write code.
see the below given topics:
Using presence in Realtime Database
where to write this given code in application or Cloud functions?
2.Connecting to Cloud Firestore
Updating Cloud Firestore's local cache
where to write local cache update code?
I use this in my android application.
Each section of the code in the Build Presence in Cloud Firestore is marked.
If it is marked with WEB, you'll need to run this code in your web application. If your clients are native Android or iOS, you'll need to write and run similar code for those platforms.
If the code is marked with NODE.JS, you will need to run this code in a Node.js environment. It should be possible to accomplish the same in Cloud Functions, but no sample is provided for that.

Resources