Getting started with Google Cloud Functions using NODE.JS

Yogeshwar Tanwar

--

What is serverless computing architecture ?

A 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 provider. You no longer have to think about scaling and maintaining servers to run your applications, databases, and storage systems

Reference — https://cloud.google.com/functions/

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.

Lets 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 initialise project:

  1. Run firebase login to log in via the browser and authenticate the firebase tool.
  2. Go to your Firebase project directory and initialise it using firebase init

and it will ask you to choose project/Create new project here.

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.

Now you should have structure as below

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

  • functions/package.json: Contains a list of NPM package dependencies of 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 , lets 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.

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

It will be something like this

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 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.

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

console logs output

Firebase Cloud Functions isstill 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 apis like Stripe.

Inspired from -

https://www.toptal.com/nodejs/serverless-nodejs-using-google-cloud

--

--

Yogeshwar Tanwar
Yogeshwar Tanwar

No responses yet