Firebase Functions | Why you should learn firebase functions ? | How to create a firebase function ?

Bhusal Bishap
wesionaryTEAM
Published in
5 min readFeb 4, 2020

--

src : https://firebase.google.com/docs/functions

The main reason for using firebase function is that it integrates perfectly with the other google cloud features. We can use it with different triggers like Real-time database triggers, cloud firestore, authentication, etc.

Why should we learn firebase functions ?
Using firebase functions is great because once deployed, we do not have to worry about the computing resources as they are automatically scaled up. We do not have to worry about the credentials, server configurations, provisioning new servers, etc. Cloud functions are fully insulated from clients which makes it one of the secured technology to be used.

Major Features :
1. Integration
2. Zero Maintenance
3. Security

How Does it work ?
After we write a function and deploy it to the firebase hosting, we can immediately use that function by calling it directly or over an HTTP request. So we as a developer can create a function by defining the behavior and conditions. Then we publish the function so its available to be used. Then, when an event is triggered, the function is invoked.

Simply , if we want to trigger a function whenever somebody tries to add something in the cloud firestore, we create a function, deploy the function and invoke the function whenever any change occurs.

If the function is busy handling many errors then google will create more instances to handle the work faster and if the function is idle, instances are cleaned up.If the function is updated, instances for the old version are cleaned up and replaced by the new instances. If the function is delete, all the instances are cleaned up and connection between the function and the event provider, i.e cloud firestore is removed.

Creating your first function :

Prerequisites :
1. Node JS ( We can install it from nodejs.org )
2. Create a project folder in local-storage (In my case it’s my-app)
3. Firebase CLI ( npm install -g firebase-tools )

In CLI : firebase login

After Successful login we will see this page in browser.

Now before proceeding any further, we have to create a firebase project which we will be using along.

Go to :
1. https://firebase.google.com/
2. Click on Go to console.
3. Click on Add Project.
4. Provide a name for the project and continue.

Within a couple of seconds, you will see this page on completion.

Now, that the firebase project is created, we are ready to configure the firebase functions.

In CLI : firebase-init functions

The firebase-init command creates a firebase.json configuration file in the root of our project directory which is required to deploy assets with the firebase CLI as it specifies which files and settings from our project directory are deployed to our firebase project.

Select Use an existing project
Select the project
Choose the language. In my case i am using javascript
Select Yes, and wait for dependency to get installed.

When the dependency gets installed, open the project in IDE. Now, we are going to see couple of files and folders.

  1. .firebaserc
    This file will continue the projects we want to configure and also help us to quickly switch between projects using the firebase use command. .firebaserc is a file that stores our project aliases.
  2. firebase.json
    In this file we describe the properties of our project. The firebase.json file is required to deploy assets with the firebase CLI as it specifies which files and settings from our project directory are deployed to our project.
  3. functions (folder)
    Inside functions folder, the first thing we have is our node modules.
    - index.js file , which is a main source file for cloud functions.
    - package.json file, this is a file describing our cloud functions.

To create a function, click on index.js file, we can simply uncomment the helloWorld function provided there.

Here, we have a helloWorld function which is going to be triggered when we request it using an HTTP request and it is going to respond to our browser with the response.
To make the function accessible, we use the exports keyword and the function name. After then we define function is a callable function or if its a function we can trigger on request.

To deploy the function :

In CLI : firebase deploy --only functions

After the function is deployed, we can see this in our terminal.

And we can check it in our firebase console.

This shows that our function is successfully deployed.

And, this is just the beginning. We can learn more about firebase functions in https://firebase.google.com/docs/functions .

--

--