Tuesday, October 17, 2017

Introduction to Cloud Functions for Firebase

Introduction to Cloud Functions for Firebase

* Before you start reading this if you want to know why we need cloud functions for firebase please check the following article: 


- Go to https://console.firebase.google.com and create a firebase project.
- Node and NPM should be installed in your local box.

Install Firebase Command Line Tools:

- Execute the following command in command line.

Rajas-MBP:Firebase raja$ sudo npm install -g firebase-tools
Rajas-MBP:Firebase raja$ firebase --version

Login to firebase from console :

- Create a project folder in you localbox called functions-firecast
- Enter inside the folder and login to firebase using CLI. Follow the command below.

Rajas-MBP:Firebase raja$ mkdir functions-firecast
Rajas-MBP:Firebase raja$ cd functions-firecast/
Rajas-MBP:functions-firecast raja$ firebase login
? Allow Firebase to collect anonymous CLI usage and error reporting information?

- Browser will open, enter the same gmail account info using which you have created the project earlier
- Close the browser after completion of the steps.

Waiting for authentication...

✔  Success! Logged in as dummymailidtest@gmail.com

* Note : You will see your gmail id instead of dummymailidtest@gamil.com *

Initialize the Project:

In the command line enter into the project folder and run the following command.

Rajas-MBP:functions-firecast raja$ firebase init

- choose the project that you have created earlier as default project
- choose Functions to configure and deploy cloud functions.
- install dependancies using npm

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...

✔  Firebase initialization complete!

Open the code in editor :

- Go to functions-firecast -> functions -> index.js and uncomment the "exports.helloworld" function.
- This http function will be triggered when there is a web request. It uses express framework of node.

Deploy the function:

- Run the following command in command line to deploy the function to google cloud under your project.

Rajas-MBP:functions-firecast raja$ firebase deploy

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/test123456/overview
Function URL (helloWorld): https://us-central1-test123456.cloudfunctions.net/helloWorld
Rajas-MBP:functions-firecast raja$

Check the content in browser :

- Open the URL https://us-central1-test123456.cloudfunctions.net/helloWorld in browser.
- You can find “Hello from Firebase!” in the browser

*Note-: Instead of test123456 you will see your project name*

Change the content and verify in browser  :

- Change "exports.hellowworld" function content as given below in index.js.

exports.helloWorld = functions.https.onRequest((request, response) => {
 response.send("Hello from Me !!");
 console.log("hello world function triggered !!");

- Save the code
- Go to console and deploy using command "firebase deploy" from your project folder.
- Refresh the browser.
- You can find the updated content !!

To find functions and Logs in Google Cloud :

- Go to the project page in google console. (https://console.firebase.google.com/project/test123456/functions/list)
- Go to the functions tab in left hand side panel.
- You can find the hello world functions in the dashboard and logs in the logs tab.
- You can see the console.log statement inside.


- Setup

1) Create a firebase project at https://console.firebase.google.com
2) Node latest version should be installed
3) NPM latest version should be installed
4) Install firebase-tools using npm : 
    sudo npm install -g firebase-tools

- Run the following command to deploy cloud function from scratch :

1) Rajas-MBP:Firebase raja$ mkdir functions-firecast
2) Rajas-MBP:Firebase raja$ cd functions-firecast/
3) Rajas-MBP:functions-firecast raja$ firebase login

After Successful Login

4) Rajas-MBP:functions-firecast raja$ firebase init
5)  Rajas-MBP:functions-firecast raja$ firebase deploy

6) Go to the http endpoint URL in browser (for ex: https://console.firebase.google.com/project/test123456/functions/list)
You can find “Hello from Firebase!” in the browser

* Got to the https://firebase.google.com/docs/  and https://cloud.google.com/functions/ for more information *