What Cloud Functions for Firebase can do
The Firebase SDK for Google Cloud Functions enables you to extend other Firebase products, giving users the features they need in your apps. One of our earliest customers, Posse, makers of the Hamilton app, needed to create a lottery system to give fans a chance to win tickets to the massively popular Broadway play - without the overhead of managing their own servers. When a user enters the lottery, Functions powered logic runs in the cloud with results stored directly to Cloud Firestore. Lottery results are pushed automatically to lucky fans’ Hamilton mobile app.
Posse also needed to scale the Hamilton app. When the lottery opens, the app’s usage can suddenly surge multifold before normalizing a few minutes later. Functions automatically scales without needing any action from Posse, and their client only pays for the resources they use.
In addition to ticket lotteries, Posse also used Functions to integrate with other Firebase products to:
- Automate creation of user profiles with Firebase Auth
- Send push notifications with Firebase Cloud Messaging
- Auto-resize user photos with Cloud Storage
- Process in-app payments using Stripe via HTTP webhooks
There’s a great deal of things you can do with Functions. For more use cases and sample code, see our popular GitHub repo.
Today’s v1.0 release comes with a number of new features based on your feedback - here’s the lowdown!
Mobile client SDKs
One of your biggest requests was for an easy way to directly call Functions from your client apps. To facilitate that, we’re adding a new type of function, along with SDKs for iOS, Android, and web clients. The new type is a Callable HTTPS function that manages an HTTPS request from the client SDK. Callable HTTPS functions drastically reduce the amount of boilerplate code you have to write to make HTTPS requests to Functions.
Here’s how it works:
- First, write a Callable HTTPS function that accepts a data object from the client. The function returns another data object to the client.
- Then, in your mobile app, you use the client SDK to invoke the function. The client SDK manges the HTTPS connection and automatically serializes the input and output data objects. In the request, the SDK will include a user ID token from Firebase Authentication if a user is logged in. Also included is an instance ID token used by Firebase Cloud Messaging.
For more, check out our docs!
Easier unit testing
While Functions Shell gave you the ability to interact with your functions locally, it was not optimal for writing unit tests such as those that run in a CI system. That’s why we are launching a new firebase-functions-test npm module that simplifies writing unit tests. firebase-functions-test takes care of the necessary setup and teardown, allowing easy mocking of test data. Now, you can either write completely offline tests that have no side effects, or tests that interact with a development Firebase project - where you can observe the success of actions such as database writes.
Official support for functions emulation
Last fall, we introduced the ability to emulate your functions locally via the Firebase CLI through both the “firebase serve —only functions” and “firebase experimental:functions:shell” command. These were experimental features, and now we are officially supporting them. “firebase experimental:functions:shell” has been renamed to “firebase functions:shell”, and “firebase serve” will emulate all HTTPS functions by default without the need for the “—only” flag.
Improved monitoring tools
After deploying functions, you may be wondering: “What’s happening with my functions? How is my app performing? Is anything broken?” With today’s release, you can now keep tabs on any errors in your functions via the new health monitor:
The APIs for writing Cloud Functions with the Firebase SDK have changed in this v1.0 SDK release. This means that upgrading to the new SDK will require some updates to any code you’ve already written during the beta. But don’t worry - this doesn’t affect any functions you have already deployed. To see what has changed and how to adapt to the new format, just follow our migration guide!
Getting started with Cloud Functions for Firebase
If you’re just getting started with the Firebase SDKs for Cloud Functions, try following our step-by-step Codelab and visiting the documentation. There is also a video tutorial to help get you set up using TypeScript as the recommended programming language.
We hope you find these new features helpful. While the Firebase SDKs for Cloud Functions are fully released, the Cloud Functions platform is still in beta. Stay tuned for more updates!