Firebase function help

So I followed the example and got the route working in Firestore (Route Tutorial - Google Cloud Platform - Blues Wireless Developers) but my issue is that I’m not familiar with Python and I’m trying to make it work for the Firebase Realtime Database using Nodejs instead.

Was wondering if there’s any guides that you could point me in?

I know how I want to organize my data in JSON so I’m ok on the note hub side, it’s just the receiving cloud function is a bit outside my skill level.

Specifically this block of code and what the accompanying package.json would contain. Thanks for any pointers.

from google.cloud import firestore

def add_data(request):
    request_json = request.get_json()
    db = firestore.Client()
    try:
        db.collection(u'notecard-data').document().set(request_json)
        return '', 200
    except:
        return '', 500

Hi @abari,

It should be relatively painless to use Node in place of Python here. It’s been a while since I’ve been in Node land, but I would start with the Cloud Firestore docs for Node.js and look at the Node.js code examples. Really all you are doing in this code sample is taking data from the Notehub POST and adding it to a Firestore collection.

I hope that helps!

Rob

1 Like

Hi Rob thanks that does help. I just picked Node because that’s what Google posts most of their examples in for functions.

If you could help me to update the Notehub sample code to import to a realtime database instead of Firestore that would definitely help get me started and I’d appreciate it a lot :pray:

Ah sorry about that - I completely missed your question about using this with Firestore Realtime Database instead! Unfortunately I have no experience with Realtime Database, though from what I can see it’s basically just a NoSQL data store.

What’s intriguing about this, though, is it looks like you can POST directly to their REST API from Notehub:

https://firebase.google.com/docs/database/rest/start

This means your route could potentially send data directly instead of using a Node function to process the data…? Interesting!

1 Like

I didn’t even think of that! I’ll have to do some reading thank you again Rob.

Playing around with postman sending test api’s to my DB URL “https://xxxx-xxxxx.firebaseio.com/raw-data.json” I can send the raw data via a POST and at least that gets the data into my Firebase DB. It’s pretty limited in what I can do without being able to dynamically update the URL, which I don’t believe note hub events supports correct? If I could dynamically update the URL then I could use the API for updates as well.

So I think my next best option is just dumping the raw data into Firebase using the POST’s above, and then setting up a function in Google Cloud to read/parse, re-organize/re-save, and then delete the raw data. Thoughts?

Or would it be better off to just do it all in the google function instead of half rest api and half function?

Sorry for all the questions Rob and I really appreciate your help.

I’m not sure if this will be enough, but we do offer the ability to include substitution variables in the URL of your endpoint. It’s not well-documented (yet), but here is a list of variables you can use.

For example, I might choose to use the [sn] variable, which means Notehub will swap in the serial number of the Notecard when the route is executed:

https://webhook.site/89bd3bc4-f8bb-4f7d-b888-3683af6da958/[sn]

…which resolves to, in this case, the device UID…

https://webhook.site/89bd3bc4-f8bb-4f7d-b888-3683af6da958/dev%3A868050040247757

Like I said, I’m not 100% sure using these substitution variables will be enough for you to customize your endpoints, but it’s worth a look.

Rob

2 Likes

Haha you’re amazing Rob, I didn’t know that was possible.

I think that will actually work for me, the most important thing I needed was the device id being part of the URL which is supported using [device] so that way my data is organized by device! At least covers me keeps my data organized until I figure out how Google functions work!

Thanks again for the all the help.

1 Like

FYI this worked perfectly with one minor note:

[device] - Replace this portion of the topic with the Notecard’s DeviceUID, stripped of the “dev:” prefix.

But including it in the webhook url it included the dev: prefix.

Not a big deal i just set the serial number of the device to dev id minus the “dev:” prefix, so its just numbers and then updated my webhook url to use [sn] instead of [device].

Thanks again.