Building Scalable Applications with Google Services Part 1: Cloud Functions and Firestore
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.
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.
- Go to https://console.firebase.google.com
- Complete the register steps
- On redirect Create a Firebase Project
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
- Navigate to the Database on the left sidebar of the Firebase Console.
- 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.
- 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.
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.
- Node.js 8 or later should be installed
- 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.
- Install Firebase CLI by running
npm install -g firebase-tools.
firebase loginto log in before running
firebase initgrants 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.
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.
- 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.
- 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`
- 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
Go get a cup of coffee while your dependencies install. You’ve done a great job!
code .in the CLI and view your work so far in VS Code.
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
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.
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!
And Puff, Cloud Functions now works and anyone can access those Functions.
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.