Vicksheet Shanbhag
3 min readMar 10, 2023

Writing Serverless Functions in Firebase: Tips and Tricks

Firebase is a popular backend platform that provides various tools and services to build web and mobile applications. One of the key features of Firebase is its ability to store and manage data in real-time. In this blog post, we will discuss how to write functions in Firebase, which allows you to extend the functionality of your application.

Firebase Functions are a serverless solution for executing backend code without the need to manage infrastructure. Functions can be triggered by various events, such as an HTTP request or a change in a database document. In this way, you can respond to events in real-time and perform tasks such as sending notifications, performing calculations, and executing third-party APIs.

To get started with writing Firebase Functions, you first need to create a Firebase project and enable Functions. Once you have done that, you can create a new function by running the following command in your terminal:

firebase init functions

This command will create a new directory called “functions” in your project’s root directory, which will contain a sample function and a configuration file.

The sample function demonstrates how to create a basic HTTP trigger function, which responds to an HTTP request with a “Hello World” message. You can modify this function to perform any task you want. For example, let’s say you want to create a function that sends a notification to a user when a new document is added to a database collection. Here’s how you can do it:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.firestore.document('notifications/{notificationId}').onCreate((snapshot, context) => {
const data = snapshot.data();
const payload = {
notification: {
title: data.title,
body: data.body
}
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic("notifications", payload, options);
});

This function listens to the “notifications” collection in Firestore and triggers when a new document is added. It then retrieves the data from the new document, constructs a notification payload, and sends it to a topic named “notifications” using Firebase Cloud Messaging (FCM).

In this example, we also initialize the Firebase Admin SDK to access Firebase services from the server-side environment. We use the admin.messaging() method to send the notification to the topic, which can be subscribed to by any interested client.

To deploy your function to Firebase, you can run the following command:

firebase deploy --only functions

This command will upload your function to Firebase and make it available to be triggered by events.

In conclusion, Firebase Functions provide a powerful way to extend the functionality of your application without the need to manage servers or infrastructure. By writing functions in Firebase, you can perform complex tasks in real-time, such as sending notifications, performing calculations, and executing third-party APIs. With the Firebase Admin SDK, you can also access other Firebase services, such as Firestore and FCM, from the server-side environment.

Vicksheet Shanbhag
Vicksheet Shanbhag

Written by Vicksheet Shanbhag

I am a Software Developer and currently work as a Full Stack Developer. I like to research about new technologies and share any knowledge or tips that can help.

No responses yet