Category Archives: Cloud

Getting started with Google Cloud Functions using NODE.JS

Getting started with Google Cloud Functions using NODE.JS

Serverless-architecture

What is Serverless Computing Architecture?

Serverless architecture is a way to build and run applications without having to manage infrastructure. Your application still runs on servers, but all the server management is done by the provider. You no longer have to think about scaling and maintaining servers to run your applications, databases, and storage systems.

How it works

What we need to do

1. Write the code

2. Upload the code to the cloud service provider

3.Initiate the trigger

After that our service provider will take care of

* Running and maintaining the server

* Scaling according to needs

Isn’t this useful for indie developers who can totally focus on developing apps without worrying about handling servers?

Let’s get to work now – Set Up and Initialize Firebase SDK for Cloud Functions

 

First, install the Firebase CLI as described in the Firebase CLI Reference. The Firebase CLI requires Node.js and npm, which you can install by following the instructions on https://nodejs.org/. Installing Node.js also installs npm.

Cloud Functions runs Node v6.14.0, so we recommend that you develop locally with this version. Once you have Node.js and npm installed, install the Firebase CLI via npm:

npm install -g firebase-tools

Now initialize project:

    1. Run firebase login to log in via the browser and authenticate the firebase tool.

    2. Go to your Firebase project directory and initialize it using firebase init and it will ask you to choose a project/Create new project here.

Firebase Project Directory


3. The tool gives you an option to install dependencies with
npm. It is safe to decline if you want to manage dependencies in another way.

 

4. The tool gives you two options for language support:

    • Javascript

    • Typescript

If you already have firebase project then, Run firebase init functions.

firebase init() function

Now you should have a structure as below

firebase structure


As you can see, the structure of the project is very simple:

  • functions/package.json: Contains a list of NPM package dependencies of the project

  • functions/node_modules: Directory in where NPM packages are installed which are mentioned in package.json

  • functions/index.js: Used for implementing Cloud Functions

  • firebase.json: Contains settings for Firebase project

 

Create cloud function

Open index.js and let’s write HTTP trigger function

const fbFunctions = require(‘firebase-functions’);

exports.welcomeToFBFunctions = fbFunctions.https.onRequest((req, res) => {

       console.log(“Testing FBFunctions“);

       res.send(“You will love these cloud functions”);

});

This is very basic cloud function based on HTTP trigger which is sending response when it is getting triggered on calling the functions.https.onRequest method

To try this, let’s deploy our project to firebase.

firebase deploy –only functions –project projectId Create a project via going to google console and get projectId from there or select project at firebase init command.

— the only flag is directing to deploy functions only for existing firebase project

It will be something like this

create cloud function

 

If deployment is successful, you can see the function URL, copy it and paste it in Browser and you will see the below output :

 

function URL

Function URL triggered the cloud function and it returned output as expected.

Open your firebase console window and navigate to functions menu, there you can see your deployed function.

firebase console

 

Debugging

 

Firebase integrates with the Stackdriver Logging service. All console logs and errors will get logged here.

Switch to Logs tab and here you can see console logs

 

 

Debugging

Firebase Cloud Functions is still in beta. It is still being improved and updated frequently. If you are planning to use Firebase Cloud functions, keep an eye on the changelogs to avoid any breaking changes in the implementation.

 

Use Cases 

 

Firebase Cloud Functions are a very useful and powerful tool. There are many use cases for Cloud Functions. Take a look at the following list of possible use cases:

  • Integrating with third-party services like sendgrid and etc etc

  • Send notifications in case of events like user signup and login

  • Send confirmation emails to users

  • Send bulk emails to users

  • Process payments with third party API like Stripe.