0

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...
    },
  );

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
c.fogelklou
  • 1,781
  • 1
  • 13
  • 26

2 Answers2

3

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.

Jose V
  • 1,356
  • 1
  • 4
  • 12
0

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.

Louis Coulet
  • 3,663
  • 1
  • 21
  • 39
  • I appreciate your answer and I have read the link that you attached and actually can't see anywhere where it says that the functions emulator will only work with an emulated database. The link attached seems to only refer to how to emulate the database, but I don't want to to that. I'm not saying you're wrong, just that the link you attached doesn't appear to explicitly that functions will only run with a RTDB emulator. Thanks. – c.fogelklou Dec 30 '20 at 12:02