DevLog.at

Using Firebase Functions

My side project uses Firebase, and wow has it come a long way since I first tried it several years ago.

We're building a mobile app. Some of our data is sourced by 3rd party APIs. Since those APIs have limits, we don't want the client to access them directly, as that situation could easily break those limits. So instead, we're going to have a Firebase function (basically an AWS Lambda – which came first?) retrieve data from the API, if and only if that data is not present in our database.

"Serverless", in other words. That is the appealing part about using Firebase – you use their features directly, instead of building them yourself, into your own server. But how will it play out in practice?

12d

Grandly, firebase has a command line tool for generating a "backend project" – files and directories for working with some Firebase code locally. This is good, as one of the major complaints I have against "serverless" is its substandard deploy processes (also a major reason why serverless.com emerged from the open source world).

Perhaps more importantly, that same dev tool allows you to run functions locally. Let's see if I can get that to work.

12d

firebase emulators:start is the command to run functions (and other firebase things) locally. It seemed to work the first time, but I couldn't tell if it was picking up my changes. Shutting it down and starting it up again, I see this error:

Port 8080 is not open, could not start firestore emulator

An interesting error, as the previous terminal output started a server that listened on that port. In fact, the command had started several servers, all listening on different ports.

This makes me think one of those servers did not shut down correctly when I hit ^c. I have to debug this now...

12d

Looks like Firestore is the service that binds to port 8080 by default.

12d

No idea what type of running process this is. I'll have to find it though some kind of port inspection.

12d

It's Java.

$ lsof -i tcp:8080
COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    53305   me     67u  IPv6 0x746e730be3e64afb      0t0  TCP localhost:http-alt (LISTEN)

12d

Ok, emulators are working again.

12d

Nice, it works now! And it does pick up changes automatically (I think my TypeScript config was wrong).

It's convenient how the cli lists the available function urls in the terminal. Much less guessing in that regard.

12d

Welcome to DevLog

The open thought platform for developers.

Share your work as you work on it. Easier than a blog, handier than Twitter.

Sign in and write down valuable thoughts that would otherwise be forgotten.