Building Scalable Applications with Google Cloud Services Part 5: Microservices with Firebase Cloud Functions P2.

Abraham Jr. Agiri
May 18 · 3 min read
Firebase Cloud Functions

Introduction

Hey there, if you are reading this you’ve successfully read through all articles from this series of Building Scalable Application with Google Cloud Service: The Firebase Cloud Series. Congratulations. 🔥🚀🎉

It’s been an exciting journey, but like all journey will come to an end. Now, this journey is successfully over.

We’d wrap this up by building a microservice for a book club, The Elite Readers Society. They’d want to hand the service over to their front-end developers to integrate their newly designed UI. You are expected to have the following features working in your service:

  • Authentication
  • Email Verification on new sign ups
  • Unverified emails should deny read and write access
  • Email Notification on new books arrival
  • The email should also contain details and a link to view the newly added book
  • Basic knowledge of Firebase Cloud Firestore, Firestore Cloud Functions, and Triggers. See the Previous Articles
  • Node.js Express
  • Typescript
  • Node.js
  • NPM
  • Firebase Tools
  • VS Code (Code Editor)

Let’s Get Started

To get started, first, we’d want to ensure that we are running the newest version of the installations mentioned above. We can check the versions by running:

  • npm --version for NPM
  • node --version for Node
  • firebase --version for firebase tools

You’d also want to set up a Twilio SendGrid. SendGrid will be used to send emails to members when a new book arrives or a book is added by a member of The Readers Society.

I’ve prepared the baked version of our service on GitHub, you can skip the tutorial and head over to the GitHub Repo Here 😎🤓

Let’s get coding! 🐱‍💻

First, we need to setup Firebase for Cloud Firestore and also Cloud Functions for Firebase:

firebase init

NB: Please, read up Part One and Part Two to finish Firebase initialisation

We’d also have to modify our folder structure:

src
|_api
|_controller
|_helper
|_triggers
|_index.ts

We’d try to do things a little bit different, we’d create helper libraries that are basically classes that extend to the Firebase Initialisation.

Before we do that first we need some npm packages run:

npm install --save firebase@5.9.2 @sendgrid/mail cors express request request-promise @types/request-promise

NB: Ensure when setting up Firestore that your security rules are set to strict mode and your project is upgraded to Blaze from the Free tire .

Helper classes will serve as our service but mainly define how we want to perform and what kind of operations we want to on our services.

We’ll first declare a helper function for Firebase in the helpers folder

Initialise a Firebase application across the application

Next, we’ll create a helper class for authentication auth.helper.ts . The helper class will also have actions for sending verifications emails when a new member is registered.

Next, we’d add the handler functions for authentication in controller/auth.handler.ts

Let’s wrap up authentication then move forward with microservice. We’d also add a REST APIto communicate with the helpersand handlers .

The API will be designed with Node.js’ Express.

Finally, to deal with Authentication we’d add the app to Firebase Cloud Functions. This will be in the index.ts file.

Now, test the functions with PostMan or CRUL, or any other you tool you prefer.

This my friend, is how you build simple microservices with Firebase Cloud Functions. Because of the lengthiness of this read, I’ve prepared a GitHub Repo with the other working functionalities of our service listed above. You can easily clone and see how it all works to the end.

I’ve enjoyed researching and preparing this series, but we’d need to prepare more awesome series. I’ll be looking forward to hearing from you and to also see your working projects hosted too.

Feel free to open an issue, or ask questions or even hitting me on twitter.

Abraham Jr. Agiri

Written by

Open Source Developer, Node.js TLC. Node.js Africa Lead.