# Firebase cloud function

Source: Medium

Web and mobile application often require back-end code to execute tasks like sending out notifications or processing long running tasks.

Firebase offers a scalable solution for running the back-end code in the cloud. Not in your devices. Cloud function action is called when the events are triggered in firebase.

#### Advantages of using firebase cloud function

* No need to run and maintain your own server
    
* You have an isolated code base for back-end code
    
* You only get billed for the actual executing time of your code
    
* The cloud infrastructure is highly scalable
    

#### Prerequisites

If we want to run the cloud function we need to install the below things

* Install Node.js [Link](https://nodejs.org/en/)
    
* Firebase CLI in your local machine
    
* Firebase account
    

Installing Firebase CLI in local machine

`npm install -g firebase-tools`

Log in with your firebase account from CLI

`firebase login`

Once logged in successfully initialize firebase

`firebase init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055573871/473bba83-4b86-4411-bca9-f27d4b9326a4.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055575838/e3d99e84-527b-4093-9827-4a91ffb92014.png align="left")

First, choose a project.

The next question you’re being asked is “Do we want to install dependencies with npm now?”. As we would like to add all necessary dependencies you need to say “Y” here or simply hit return as “Y” is the default setting.

Now you can see this screen in your terminal:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055577688/3f9602d2-68d5-4c36-b7cc-915bc18f935d.png align="left")

Once initialized you can navigate to your project and its structure would be like

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055579281/48e9a37e-cf5b-4cc8-8b57-b3065e6b1abe.png align="left")

In index.js file add the below code

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055580905/d68f1239-a675-4869-8d3f-8603c5137411.png align="left")

It’s time to deploy our code

firebase deploy

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055582594/4452275b-e38a-4a01-be8e-6f9ad4239448.png align="left")

If you’re opening up the current Firebase project in the back-end and click on the link Functions you should be able to see the deployed helloWorld function in the Dashboard:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698055584297/f77b357d-da8e-4938-a6ca-b04e85460d77.png align="left")
