At Google I/O, we were excited to announce that Cloud Functions for Firebase 2nd gen is now generally available to use in your production apps. First announced in beta a year ago, 2nd gen functions are more efficient, feature new triggers, and, in a first for Cloud Functions for Firebase, introduce beta support for a second language beyond Node.js: Python!
Faster, more efficient functions
This is the foundational change in 2nd gen functions, which gets its superpowers from Cloud Run. Each instance of a function can now handle multiple simultaneous requests. We call this concurrency, and it means that your functions can absorb spikes of traffic more easily than first gen, without having to spin up new instances—reducing the number of times your users will experience the dreaded cold start.
Watch Daniel, the tech lead of Cloud Functions for Firebase, explain concurrency and how to tune your functions for optimal performance in the video above.
Introducing support for Python
We’re excited to announce that we’re now in public preview with Python support for Cloud Functions for Firebase. Yep, you can now use Python to write your Cloud Functions:
from firebase_functions import https_fn
@https_fn.on_request()
def on_request_example(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello Pythonistas!")
Python support unlocks an incredible ecosystem of libraries like TensorFlow, NumPy, pandas, and more. In the video above, Jeff demonstrates how to use a callable Python function to add a PaLM-powered poem API to a web app.
Functions written in Python integrate with the rest of Firebase just like Node.js functions do. They can be triggered by numerous other Firebase products, tested in the Firebase emulator suite, and protected with App Check. Check out the Getting started guide for detailed instructions on how to get up and running with Python.
React to more events in your Firebase project with EventArc
2nd gen adds support for new triggers powered by EventArc:
- Firebase Alerts add triggers that automatically notify your team when your app has a new tester in App Distribution, a performance issue reported in Firebase Performance monitoring, or a stability regression in Crashlytics.
- Custom Events can be triggered by your installed extensions to allow you to run custom logic, or by your own custom EventArc event source
In addition to the new triggers, 2nd gen functions support favorite triggers from 1st gen, like https, callable, and Firestore.
2nd gen functions support Firestore triggers, including support for Firestore in the emulator suite.
We’re still working on supporting the Firebase Authentication onCreate
and onDeleted
events from 1st gen, as well as Analytics events, but in the meantime you can keep those on 1st gen and run them alongside your 2nd gen functions.
Upgrading to 2nd gen
1st gen and 2nd gen functions can coexist side-by-side in the same file. This allows for easy migration piece by piece, as you’re ready. When you run the firebase deploy
command, the Firebase CLI determines whether a function is 1st gen or 2nd gen based on its imports:
// 1st gen functions use a monolith import
const {functions} = require("firebase-functions");
// explicit 1st gen import works too
const {functions} = require("firebase-functions/v1");
// 2nd gen functions use modular imports
// from the "v2" subpackage
const {onRequest} = require("firebase-functions/v2/https");
Upgrading a 1st gen function to 2nd gen has 4 main steps:
- Update imports
- Update trigger definitions
- Use parameterized configuration instead of
functions.config
- Migrate traffic to the 2nd gen function
1st gen and 2nd gen functions can coexist side-by-side in the same file.
For a full guide, see Upgrade 1st gen Node.js functions to 2nd gen.
Get started and give feedback
Try out 2nd gen functions
If you’re looking to get started with 2nd gen functions, there are a few ways to get started. You can follow a detailed tutorial in the Getting started guide, browse the functions-samples repository to get an idea of what you can build with functions, or simply generate starter code with the Firebase CLI:
Let us know what you think
- Request new features on the Firebase Feature Requests Portal
- Problem deploying? Find help on the support page
- Report bugs on the open source repositories for Python and Node
Write code that responds to events and invokes functionality exposed by other Firebase features.