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.
This is a Beta release of Google Cloud Functions. This API might be changed in backward-incompatible ways and is not subject to any SLA or deprecation policy.
Learn more
Learn more about the Firebase SDK for Cloud Functions in the Firebase documentation or check out our samples.
Usage
var functions = require('firebase-functions');
var notifyUsers = require('./notify-users');
exports.newPost = functions.database
.ref('/posts/{postId}')
.onWrite(function(event) {
if (!event.data.previous.exists()) {
notifyUsers(event.data.val());
}
});
Contributing
To contribute a change, check out the contributing guide.
License
© Google, 2017. Licensed under The MIT License.