Google firebase database with google functions running locally

117 views Asked by At

I have an app connected to google firebase realtime database. I want to test google cloud functions as a server for running backend functions triggered once every day or triggered by data changed in the database.

I have an emulator instance running locally with a typescript function that I wrote. I have the realtime database running, connected to the app and working well.

Now when I change data in the database, the functions are not being triggered.

function in index.ts:

import * as functions from "firebase-functions";


exports.simpleDbFunction = functions.region('europe-west1').database.ref('/some path in the database')
    .onCreate((snap, context) => {
        console.log("functions triggered by datachange:", snap.val())
    });

the firebase project is connected to the correct project (using the interface in the terminal). the app and realtime database are currently working and I can see changes in the data inside the firebase console.

What am I missing?

I've also tried the more complicated example from google's documents (makeUppercase function). But when it didn't work I went back to a simple function that is triggered on data creation and prints to the console.

Edit: I notice that in the emulator UI the real time database is empty. Shouldn't it connect to the real time database of the app? isn't that why I connected the emulator to a specific project? Edit2: I tried changing the function line to:

functions.region('europe-west1').database.instance("the URL of the rtdb").ref(...

and I get an error saying that the event function has malformed resource member

1

There are 1 answers

0
Raz Gavrieli On

So I kinda solved it:

  1. I found out that you can't really connect from a local emulator to the production environment of your actual google firebase. (Which is kinda weird, you have to commit to this technology even before you had a chance to see any code in action) <-- fix me if I'm wrong about this

  2. If you use realtime database cloud functions you can't use any region other than 'us-central1'. as mentioned in this document: https://firebase.google.com/docs/functions/locations

  3. You can connect your app to the local emulator on your machine! Get your database instance:

FirebaseDatabase database = FirebaseDatabase.getInstance();

Add the following line in your app (which is weird) before the first database operation:

database.useEmulator("10.0.2.2", 9004);

change 9004 to your local realtime database port.