Firebase cloud functions



This is one of a series of articles about Going serverless with Firebase

What are Cloud functions

There are both Google and Firebase branded cloud functions.  They are probably the same thing under the hood, but Firebase cloud functions are easier to deal with and have a better local testing environment - meaning you don't need to deploy till you're sure it's going to work. They both seem equivalent in terms of performance and if you are using Firebase or Firestore, Firebase cloud functions are already integrated.

Cloud functions are simply Node Apps that have been deployed to Firebase. When you deploy them, their Node dependencies are resolved and a container deployed to host your app. They are invoked in a number of ways
  • Sending an HTTP request to a given address
  • Detecting a change in a database, or an authentication request
  • Detecting a given Google Analytics event
  • A change in cloud storage 
  • A Pubsub message
This example is using the HTTP request and detecting a change in a database to cause my cloud functions to run.

Getting started

You'll need to be running Node for this. If you are not already, a good solution is to use c9.io where you can provision a development environment for free. Adding firebase to your environment is easy, and it's described here https://firebase.google.com/docs/functions/get-started

What does a cloud function look like

Initially, you write your Node App as you would normally, including the dependencies in your package.json and testing it with whatever tools you normally use. When you are ready to try it out as a cloud function, you can test it locally using an emulator and when confident it will work, you can deploy it. A cloud function can be a single microservice , or as in this case, a complete app acting as an API.  

Here's how to expose working node apps as two firebase cloud functions. 
const functions = require('firebase-functions');
const efxapiv2 = require('./efxfb');
const efxapisubv2 = require('./efxsub');


// function http entry point for function
exports.api = functions.https.onRequest(efxapiv2.app);

// entry point for watching out for updates
exports.apisub = efxapisubv2.init();

The function api is a Node express app that is going to be activated by receiving an HTTP request. Pass an Express app object to the cloud functions https handler. This is will interpret and handle all requests to the api.

The function apisub is an app that reacts to changes in a given Firebase real time database and will interact with any clients that have subscribed to push notifications through the api.

To test the cloud function,  use the local emulator, started like this
> firebase experimental:functions:shell

pass a request to it as if it has been received as an HTTP request
firebase> api("/v2/info")

and get a response like this
Sent request to function.
firebase > info: User function triggered, starting execution
RESPONSE RECEIVED FROM FUNCTION: 200, {"info":{"api":"efx","version":"2.2.0","platform":"fb"},"ok":true,"code":200,"operation":"info"}
info: Execution took 150 ms, user function completed successfully

next, deploy it
> firebase deploy

and hit the cloud function address from the browser (this example is using a custom domain, with Firebase hosting, both of which are the subject of other articles)


And that's all there is to the mechanics of turning a node app into a deployed cloud function. If you are familiar with App Engine, then you'll immediately see how easy this is in comparison.

For other articles on this topic see Going serverless with Firebase



Comments