What is firebase-functions?
The firebase-functions npm package allows developers to create and deploy serverless functions that are triggered by Firebase and Google Cloud events. These functions can be used to handle HTTP requests, respond to changes in the Firebase Realtime Database or Firestore, authenticate users, and more.
What are firebase-functions's main functionalities?
HTTP Functions
HTTP functions allow you to create endpoints that can be triggered via HTTP requests. This is useful for creating RESTful APIs or handling webhooks.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send('Hello from Firebase!');
});
Firestore Trigger
Firestore triggers allow you to execute functions in response to changes in Firestore documents. This can be used for tasks like sending notifications or updating other parts of your database.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.onUserCreate = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
const newValue = snap.data();
console.log('New user created:', newValue);
return null;
});
Authentication Trigger
Authentication triggers allow you to run functions in response to user authentication events, such as user creation or deletion. This is useful for tasks like sending welcome emails or cleaning up user data.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
const email = user.email;
console.log('Sending welcome email to:', email);
// Add email sending logic here
return null;
});
Realtime Database Trigger
Realtime Database triggers allow you to execute functions in response to changes in the Firebase Realtime Database. This can be used for tasks like data validation or synchronization.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.onDataChange = functions.database.ref('/path/to/data').onWrite((change, context) => {
const beforeData = change.before.val();
const afterData = change.after.val();
console.log('Data changed from', beforeData, 'to', afterData);
return null;
});
Other packages similar to firebase-functions
aws-lambda
AWS Lambda is a serverless computing service provided by Amazon Web Services. It allows you to run code without provisioning or managing servers, similar to Firebase Functions. AWS Lambda supports a wide range of triggers, including HTTP requests via API Gateway, changes in DynamoDB, S3 events, and more.
azure-functions
Azure Functions is a serverless computing service provided by Microsoft Azure. It enables you to run event-driven code across various Azure services and third-party services. Azure Functions supports HTTP triggers, timer triggers, and triggers from other Azure services like Cosmos DB and Blob Storage.
openwhisk
Apache OpenWhisk is an open-source serverless platform that allows you to execute functions in response to events. It supports a variety of triggers, including HTTP requests, database changes, and message queues. OpenWhisk can be deployed on various cloud providers or on-premises.
Firebase SDK for Cloud Functions
The firebase-functions
package provides an SDK for defining Cloud Functions for Firebase.
Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code. The Firebase SDK for Cloud Functions integrates the Firebase platform by letting you write code that responds to events and invokes functionality exposed by other Firebase features.
Learn more
Learn more about the Firebase SDK for Cloud Functions in the Firebase documentation or check out our samples.
Here are some resources to get help:
If the official documentation doesn't help, try asking through our official support channels: https://firebase.google.com/support/
Please avoid double posting across multiple channels!
Usage
const functions = require('firebase-functions');
const notifyUsers = require('./notify-users');
exports.newPost = functions.database
.ref('/posts/{postId}')
.onCreate((snapshot, context) => {
functions.logger.info('Received new post with ID:', context.params.postId);
return notifyUsers(snapshot.val());
});
Contributing
To contribute a change, check out the contributing guide.
License
© Google, 2017. Licensed under The MIT License.