Building Scalable Applications with Google Services Part 1: Cloud Functions and Firestore

Abraham Jr. Agiri
6 min readMar 21, 2019

--

Google Services

Building scalable applications is a popular phrase in today’s web technology. Google has invested heavily in making this process seamless. Google has also invested in accessibility, dev tools, performance testing, and integrating Cloud Functions popularly called “serverless” by most companies.

Without a doubt, we could agree that Google web applications load incredibly faster than most web apps even with slow or poor internet connectivity. Googlers have taken the responsibility of making their technologies available to you. Giving you the developer the power to extend the Google Experience to your end-users. You could also see the https://web.dev for testing and scaling up the performance of your web applications.

Today, our major focus will be scaling up our servers without building any. Serverless could be difficult to adapt at first, but Google has simplified the process and difficulty of understanding serverless into a simple term called Cloud Functions.

It’s easier to live with the name Functions and simply get on-board by taking out all the fancy terminology “serverless” and replacing it with something simple to understand “Functions”. Serverless also has servers. The only difference in this architecture is you don’t bother about scaling and managing them yourself, so it’s called serverless, but see them as simple JavaScript or any programming language SDK you install and use to build more apps or API that you can make use of.

So, how do we build scalable servers with Google Services? Well, it’s quite easy.

We’ll be using Cloud Functions for Firebase and Firebase Cloud Firestore. Don’t get it confused with the Google Cloud Firestore SDK if you’re already familiar with it.

Getting started is really simple

Google has made navigation around the firebase console so easy that you can easily click around to get started. It might be a little bit difficult if you’re completely new to programming, but I promise you’ll get it right after your first few tries.

NB: The project name should be reasonable to the associate project you plan on developing. For the sake of this “onboarding” to Google services, I’d call mine gservice-getting-started

Fig 1.0 Add a Firebase Project.
  • Navigate to the Database on the left sidebar of the Firebase Console.
Fig 1.1 Creating Database
  • Click on Create Database and select Cloud Firestore.

NB: Creating the project and the database is important before running the CLI. Why? The CLI won’t be able to access your project from Firebase if they don’t exist.

  • Create Security Rules. For this article, we’ll use the test mode as shown below.
Fig 1.3 Creating Firestore Security Rules
  • Click Enable and you’re ready to go.

A Firebase project can be integrated seamlessly across other Google Cloud Platforms (e.g.) App Engine, Identity & Access Management (IAM) and etc. isn’t that cool? Well, that because Firebase is also a Google Cloud Service.

Remember, Firebase lives in the Cloud and cannot be installed the same way we install MongoDB or other NoSQL and SQL providers on our machines. That’s because Firebase is NOT a NoSQL database or a database in any form.

Firebase Realtime Database and Firebase Cloud Firestore are NoSQL Databases, flexible and scalable for mobile, web, and server development from Firebase and Google Cloud Platform.

So, if Firebase is not a Database then what is? 🙄🤔 Of course, I will tell you.

Firebase is a Google Cloud Service that eases building your application, improving your application quality, and growing your business. Firebase services also include ML Kit, Analytics, Crashlytics, Performance monitoring and lots more. Firebase gives you the tools to develop high-quality apps, grow your user base, and earn more money.

And Cloud Functions for Firebase lets you automatically run backend (server-side) codes in response to events triggered by Firebase features and HTTPS requests. Google’s cloud provides a managed environment for storing and running your codes. Totally removing the need to managing and scaling up your own servers.

Firebase CLI

The Firebase CLI provides you with a way of communicating with your Firebase project in the Cloud and a way to create a Firebase project locally to facilitate that communication.

Moving on, we’ll install the Firebase CLI firebase-tools and build an app the Google Way.

Prerequisite:

  • Node.js 8 or later should be installed
  • Little knowledge of JavaScript and Node.js
  • Visual Studio Code installed

Our focus is building a scalable application with Google Services, in other words for the sake of this “on-boarding” we won’t be developing a UI yet.

Next Step:

  • Install Firebase CLI by running npm install -g firebase-tools.
  • Running firebase login to log in before running firebase init grants you access to your Firebase applications in the Cloud.
  • After completing the authentication process, you can run
  • firebase initin a new project folder and follow the prompt in the CLI.

NB: firebase init initialises a Firebase application on your machine. Following the prompts properly also setups a scaffold for easy development of a Firebase project. Our focus is on building Cloud Functions and to do that we have to select Firestore and Functions in the interactive CLI as shown below.

Fig 2.0 Creating a Project Locally
  • Use the arrow key to move up and down and use the spacebar key to select as shown in the Figure above.
  • Press Enter to proceed.
Fig 2.1 Select a Firebase Project
  • Select the Firebase Project that you’d love to develop as shown in the figure above.
  • Follow the prompts and set `firestore.rules` and `firestore.indexes.json`
Fig 2.3 Setting TypeScript
  • From the Figure above I have selected TypeScript. TypeScript is highly recommended by the Google Firebase Team.
  • Ensure to select (Y) when it prompts “Do you want to use TSLint to catch probable bugs and enforce style?”
  • Also, ensure to select (Y) when it prompts “Do you want to install dependencies with npm now?”

Go get a cup of coffee while your dependencies install. You’ve done a great job!

  • Run code . in the CLI and view your work so far in VS Code.
Fig 2.4 Project Directory

I just gave myself a pat at my back for a great job so far.

One more thing to do before we complete this part and move on to building something real.

  • Uncomment the demo code in VS Code and hop back to the CLI
  • Navigate to the `functions` directory with cd functions
  • Run npm run serve
  • Copy the URL and paste in the browser.

NB: Don’t bother about the warning in the console. Google Cloud Functions support up to Node 8 Carbon in production. So, you can ignore it and proceed. The Warning message is in reference to the Emulator, not Google Cloud Functions itself.

Fig 3.0 Running Cloud Functions Locally

Hurray! 🎉🎉🎉 Congratulations you’re ready to deploy your Cloud Functions!

To deploy simply run npm run deploy once that’s done copy the URL and Paste in the browser!

Fig 3.1 Running Deployment

https://us-central1-<project-id>.cloudfunctions.net/helloWorld

And Puff, Cloud Functions now works and anyone can access those Functions.

Conclusion

Great Job! Now, we’ve gotten the basics in the next tutorial we’ll build something more real. Kindly drop comments and suggestion on the kind of application that we can build together.

We also take a deep dive into every foreigner mentioned in this article and tackle them one after the other. Do well to leave comments so we could learn further together.

--

--