Search code examples
google-cloud-platformfirebase-realtime-databasefirebase-tools

Realtime database `onValueWritten` event does not trigger in emulator


I have the following code in the onDbUpdated function:

    //source: https://firebase.google.com/docs/functions/database-events?gen=2nd
    import {onValueWritten} from "firebase-functions/v2/database";

    export const onDbUpdated = onValueWritten(
    {
        ref: "/scoobs",
    },
     (event) => {
        console.log({ event });
    });

When I start the emulator, I get the following messages in the console: console warning message

And when a new value is created in the default emulator database, the function onDbUpdated does not trigger.

  1. What does it mean that the function region is defined outside the database region?
  2. Does it mean that the database region should be "europe-west1", like the functions?
  3. If so, how should I specify the emulator database region?

Solution

  • As stated in the Documentation

    Distance between the location of a Realtime Database instance and the location of the function can create significant network latency. Also, a mismatch between regions can result in deployment failure. To avoid these situations, specify the function location so that it matches the database instance location.

    By default, functions run in the us-central1 region as per the Documentation. Note that this may be different from the region of an event source, such as a Realtime Database.

    So as per the Cloud Functions documentation the region recommended to deploy the Cloud Function if they are triggered by or uses a Realtime Database is always US-Central1.

    Another way to provide a better experience for the Non-US users could be trying to implement a function in the EU zone for all the non database related jobs that later connects to the Cloud Functions that will do all the jobs which are related to the database.

    Also have a look at another alternative way suggested by @Dharmaraj, in this Stackoverflow Link by Specify instance and path. Checkout specify the instance and path section in the documentation.