Search
Close this search box.

Cloud Functions for Firebase

Google Cloud Functions is a compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment, this is why Cloud Functions are considered serverless architecture.

Cloud Functions for Firebase provides a way to extend the behavior of Firebase and integrate Firebase features through the addition of server-side code. It is a joint product between the Google Cloud Platform team and the Firebase team.

Cloud Functions can be written in Node.jsPythonGo, and Java.

Getting Started

There are 2 distinct types of Cloud Functions:

  • HTTP functions
  • Background functions

I assume you have a prior experience using Firebase.

  1. Create or select a Firebase project in the Firebase console.
  2. Install the Firebase CLI.
  3. In new firebase project, create database
  4. gcloud auth application-default login

Initialize the Firebase SDK for Cloud Functions

Here, we create an empty project containing dependencies and some boilerplate sample code. Code can be written in Javascript and Typescript.

  1. Create a new project folder.
  2. cd /NewProjectFolderName
  3. Run firebase login
  4. Run firebase init

When complete, your project structure should look similar to this:

.
├── .firebaserc #Hidden file that helps you quickly switch projects
|               #using 'firebase use'
├── firebase.json #Describes properties for your project
└── functions #Directory containing all your functions code
    ├── index.js #main source file for your Cloud Functions code
    |  
    └── package.json #npm package file describing your Cloud Functions 
                     #code
  1. Inside /functions/index.js, lets Import required modules and initialize an app:

2. Let’s add our first Cloud Function.

This is an HTTP endpoint. Any request to the endpoint results in ExpressJs-style req/res objects passed to the onRequest callback.

This function passes a text value and inserts it into the database under the path /messages/:documentId/original

3. Let’s add a background function that modifies our data

This function listens for new messages added to /messages/:documentId/original and creates an uppercase version of the message to /messages/:documentId/uppercase

Our makeUppercase() function executes when Cloud Firestore is written to. ref.set defines the document to listen to.

  • For now, this is all we need to start testing our function

Emulator

The Firebase Local Emulator Suite allows us to build and test apps on our local machine instead of deploying to a project.

  1. cd /functions, run: npm install
  2. Run firebase emulators:start

*If you receive an error “

Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.

“, run: gcloud auth application-default login*

Using the emulator suite, we can locally test our functions:

  • Navigate to the given URL in the browser.
  • Add the text parameter, and give it a value.
Our message was created!

Deploy to a production environment

  • Let’s deploy our functions and then test a trigger
$ firebase deploy --only functions
Firebase CLI reference
Deploy Successful!
  • Navigate to console.firebase.google.com.
  • Select your project.
  • Click the Functions tab

Here we see our deployed functions with additional details.

  • Grab the URL from the HTTP function.
Our message was added to our Firestore and uppercased!

Google Cloud Functions is a lightweight compute solution for developers to create single-purpose, stand-alone functions that respond to cloud events without the need to manage a server or runtime environment. Be sure to check out some function examples below!

Firebase function examples.

Cassandra.Link

Cassandra.Link is a knowledge base that we created for all things Apache Cassandra. Our goal with Cassandra.Link was to not only fill the gap of Planet Cassandra, but to bring the Cassandra community together. Feel free to reach out if you wish to collaborate with us on this project in any capacity.

We are a technology company that specializes in building business platforms. If you have any questions about the tools discussed in this post or about any of our services, feel free to send us an email!

Photo by Reza Rostampisheh on Unsplash