Building Scalable Applications with Google Cloud Services Part 2: Google Cloud Function
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:
- 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.
- 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
- Locate your ‘Hello World’ Cloud Function locally
- Log into your Firebase Console https://console.firebase.google.com
- Open or create a project (please, use the project from the previous section)
- Follow the diagrams below to complete creating a Firestore Collection and Document (I will explain this shortly).
NB: Leave Field and Value blank and save.
index.tswith the following codes.
- Read the code properly to understand what’s going on in there!
- Locally serve the functions with
npm run servemake sure nothing is breaking.
- Either use cURL or Postman to test the request
- Add more code to
index.tsto GET chats from Firestore
- Add background triggers to change ‘advocate’ to ‘avocados’ 🥑
NB: Remember to save the index file as
- 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:
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.
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! ❤❤