Firebase Cloud Functions 101
Firebase Cloud Functions: Creating your first serverless application in 10 minutes
Have you ever wanted to add some extra Google features to your application but don't want the hassle of managing service or infrastructure? Well meet Firebase Cloud Functions, and it allows you to run your backend code without worrying at all about the infrastructure behind it.
In simple words, it’s a service from Google that would easily allow you to run code on certain events like a new user signup or a database change in your application.
You can write basic functions with javascript and Firebase Function will take care of everything in terms of deployment and scaling without you having to worry about it.
So you can easily add cool features like processing images or sending push notifications without worrying about the technical details.
Let's try it out right away, and I'm sure you'll be impressed. My goal for this article to help you is to create a cloud function from scratch in less than five minutes.
We’ll start with a basic Hello-World application.
We'll do a firebase init
, which will initialize our application with no code in the beginning, we'll deploy it and even test it out by invoking the API call that we’ll create. So let's get into the code right away.
Two prerequisites for this are:
Have an account on Firebase.
Switch to a paid account, which is also known as Blaze Plan.
However, don't worry about getting any bills or spending actual money. Wondering why? See the pricing policy below:
So up to 2 million requests a month. It's free. You actually only need to pay if your request exceeds 2 million a month. I'm sure you won't go over 2 million unless you're creating a huge application that's very popular (and that’s a good problem to have).
Now, open up your code editors and the first thing you need to do is run this command:
npm install -g firebase-tools
This will install all the necessary Firebase libraries that you need in order to run the Firebase commands on your terminal or command line.
The next thing we need to do is run
firebase login
This will ask for your credentials and then log us into our Firebase account.
This is where the fun begins.
Now we have to run
firebase init
which will actually initialize our Firebase project, and it'll ask us, what services would we like to use, select “Function”. Follow the next few simple steps and your project setup is now complete.
Here are a few screenshots of my setup
Once the setup is completed, you will notice a functions folder. In this folder, we have a package.json file, a node_modules folder, and the index.js file (this is where the code lies).
index.js is the heart of our application
Start with this import:
const functions = require("firebase-functions");
The “firebase init” command took care of installing all the dependencies, including firebase-functions.
Create an actual firebase function now with just 2 lines of code next:
exports.helloFunction = functions.https.onRequest((req, res) => {
// your code goes here
});
So whatever function you export here, will be the name of the Firebase function that is actually called when invoked, which is essentially a serverless application. This function can have hundreds of lines of code doing complicated tasks, or simply a “hello world” application.
exports.helloFunction = functions.https.onRequest((req, res) => {
res.json("Hello World from a serverless application.")
});
All we are doing is just returning a response in JSON format, which will contain a message. Now you must be thinking “how to actually deploy this function”, right? Is it very complicated? The answer is not at all.
I just have to run this command:
fireplace deploy --only functions
That's it. We have successfully deployed. Congratulations!
Your API is enabled and soon it's gonna register a REST-API with “helloFunction” as an endpoint.
You have created your very own Firebase Function and simply test it out using Postman.
I am amazed to see how simple it is to create a serverless application today.
My mind was blown the first time I did this because I used to think serverless applications are a mystery and they must be complicated. We all have heard about the AWS Lambda function, which is essentially the same thing, but by Amazon and Firebase-Functioin is by Google.
I hope you also have bridged the gap of knowledge today in order to launch a first-ever serverless application or a firebase function in this particular case, and I hope this will encourage you to create your own (and much better) applications.
Obviously, this is just a hello function, but as I said, if you want to learn more about Firebase Functions, check out our YouTube tutorial videos:
Also, feel free to ask your questions. We try to reply to the majority of comments that we get on our articles/videos.