Building Scalable Applications with Google Cloud Services Part 2: Google Cloud Function

Firebase and Cloud Functions

From the previous article, we established a concrete understanding of what Functions are and why adopting and leverage Google cloud services is efficient for building scalable applications.

Quickly before we dive into ‘Part 2’ of the article series, let’s have a little recap of what we’ve been able to cover so far. Below are some key areas we focused on:

RECAP

  • Defined Functions and Serverless Architecture
  • Defined Firebase Firestore, and Firebase Realtime-Database
  • Used Firebase Console and CLI to create a simple project
  • Deployed a simple “Hello World” Cloud Function

We can immediately advance a little bit further and grapes some major concepts about Google’s Cloud Functions.

Google Cloud Function

Google’s Cloud Function, is developed by two teams:

  • Firebase Team
  • Google Cloud Platform Team

In other words, we have Google Cloud Function for Firebase developers and Google Cloud Function for Google Cloud Platforms developers. Google Cloud Function is Google’s serverless compute solution for creating event-driven applications. A joint product between the Firebase team and the Google Cloud Platform team.

What does that even mean? 🙄

Simple. For Google Cloud Platform developers, Cloud Functions serves as a connective layer that allows a developer weave logic between Google Cloud Platforms (GCP) services by listening for and responding to events.

For Firebase developers, it provides a way to extend the behaviour of Firebase and integrate Firebase features through the addition of server-side code.

In this ‘Section’ of the article, our focus will be using Cloud Function for Firebase: leveraging Cloud Function to build a serverless application with Node.js.

Let’s build a simple chat application for developers.

Task:

  • Use Firebase admin to initialize the firebase application
  • Trigger Functions with HTTP requests to add and get messages from Firestore
  • Create background functions that get triggered when a message is created or updated
  • Change the word ‘advocate’ to an ‘avocado’ when a new chat is added to Firestore or updated in Firestore

Solution

Step 1:

Step 2:

  • Follow the diagrams below to complete creating a Firestore Collection and Document (I will explain this shortly).
Fig 1.0 Create a new collection with Firebase Console.
Fig 1.1 Create the Chat Room Collection.
Fig 2.0 Create a Unique Document for Developer Chats
NB: Leave Field and Value blank and save.

Step 3:

  • Update index.ts with the following codes.
  • Read the code properly to understand what’s going on in there!
Code 1. HTTP request Function
  • Locally serve the functions with npm run serve make sure nothing is breaking.
  • Either use cURL or Postman to test the request
Fig 3.0 Test HTTP request to add chat to Firestore
  • Add more code to index.ts to GET chats from Firestore
Code 2. HTTP request function
Fig 3.1 Test HTTP request to fetch Chats from Firestore
  • Add background triggers to change ‘advocate’ to ‘avocados’ 🥑
Code 3. Background Functions trigged when a message is created or updated
NB: Remember to save the index file as index.ts

Step 4:

  • Deploy functions to Firebase npm run deploy
  • Repeat all test with Postman as shown above
  • Ensure you have no error
NB: You can cross check your codes with the GitHub Repo

Cloud Function Lifecycle

Phew! this was a long ride to getting all this work done! We need some ice-cream🍨🍧🍦 to make us feel a whole lot better, don’t you think so? 😁

Learning won’t be complete if don’t understand what just happened in the codes above. And trust me, they were lots of magic going on in there! But let’s take a look at the diagram we have below to understand better:

Fig 4.0 Cloud Function Lifecycle

That there is the typical behaviour of Cloud Functions and all the mystical codes we’ve added to index.ts that got deployed to Firebase.

Fig 5.0 Firebase Console and Deployed Functions

Conclusion

In our next article, we’ll do more justice to Cloud Functions and more on Firestore Data Structure. Till then don’t forget to clap, share, and to check out Firebase docs.

I’m always happy to review your codes, and talk if you eventually run into any trouble when using Firebase or Node.js! My dm on twitter (@codeekage)is always open!

See you soon! ❤❤