Firebase Cloud Functions - your secret Node.js server

Firebase Cloud Functions - your secret Node.js server

Hannu Kuusi bio photo By Hannu Kuusi

Recently Firebase launched it’s Cloud Functions beta, which plain and simply is Node.js server running in the cloud. This came just in time for me as I needed a backend server for the food delivery app I was working with. Now Firebase runs all the backend code I needed and does an amazing job at it. I can honestly say it was love after my first ‘firebase deploy’ and the instant relief of how nicely things just worked from there on.

Summary, from their Github samples “Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code. Cloud Functions for Firebase integrates the Firebase platform by letting you write code that responds to events and invokes functionality exposed by other Firebase features.”

What this means is that your code is stored in Google’s cloud which is fully managed environment and you don’t need to lose hair over any server setup or upkeep stuff. Development is done locally with your favourite editor and the code is deployed using Firebase Cloud Functions CLI. While everything usually has to have some downsides I’m currently more than happy using Cloud Functions for everything my latest project needed from the server-side. It’s currently handling all the orders coming from customers, mapping the delivery locations, receiving updates to the product data from admins, sending push notifications and so on.

The main magic with Firebase Cloud Functions is that it can watch for changes in the data stored at Firebase. So, for example when an order status is changed, my “notifyCustomer” function fires. It checks the new status from the order data, pulls the user ID from there and then checks if we have registered a push-notification ID for that user, if we do the user is then sent a notification about the change in the order status. Same happens if a user referred by someone places his first order, the referrer is notified about being given a nice bunch of bonus credits for referring a new paying customer.

Also as my client had their product data in excel sheet it was a breeze to bring it over to Google Sheets, share the access with client and then automatically watch for any changes. When a change is detected, the product data in Firebase is then changed accordingly. This was probably most cost effective and straight forward product management system I’ve ever done, mainly as I did so little and there was no need to explain how Google Sheets works. A change or addition to product sheet shows up to all customers using the app in real time, it takes roughly 2-3 seconds for changes to take effect in Firebase.

I really suggest to take a look at this as your next server-side solution, it’s not a do-it-all magic trick but can probably handle most things with ease. Also, it’s free for most needs (see Firebase pricing). My main reason for upgrading is that outbound (out of Googles network) connections are not allowed in the free tier, and I like to have the Slack notifications coming when important stuff happens on Firebase.