Firebase provides many services. One of them is Cloud Functions. In this post, we are going to learn how to use cloud functions.
Understanding the problem that cloud functions solve.
I was working on a project in which I need to implement Push Notification whenever we get a new entry in the firebase database. For this, I need to execute a function that makes an API request to a URL.
Now to achieve this functionality you have to create an API run it on a server and keep track of new entries and so on.
Here Cloud Functions come in handy. You just need a function that performs action whenever this event occurs. That's it no backend and nothing fancy.
Implementation
Here comes my favorite part. We will go through step by step.
Here I'm considering that you already have a working project.
1) Install Firebase CLI
npm install -g firebase-tools
2) Login to Firebase
firebase login
3) Initialize Firebase Project.
firebase init
Note:- Keep all options default and choose Functions: Configure and Deploy Cloud Functions
for features to use.
Now in the root directory of the project, you can see a new directory called functions
which is created by Firebase.
In the functions
directory, there will be an index.js
file. You will see some already written code there.
const functions = require('firebase-functions');
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
This is default code and it sends response Hello from Firebase!
to the user. It will sound familiar if you previously worked on express
.
4) Deploy Cloud Function.
firebase deploy --only functions
To see the response navigate to your firebase project then goto functions tab and find URL under the trigger tab. Open URL and you can see the message Hello from Firebase!
.
If you are getting an error like this while deploying the function. Deploy it again it worked for me in the second deploy.
Going advance.
Now the goal was to trigger this function when we have a new entry in the Firebase database.
For this, we have to add some code to functions/index.js
file.
const functions = require("firebase-functions");
exports.sendNotification = functions.firestore
.document('posts/{id}')
.onCreate(snapshot => {
// Make api request or do anything you want.
});
Let's try to understand the above code.
First of all, there are four functions available to keep track of activity in the database.
1. onCreate => Triggered when a document is written to for the first time.
2. onUpdate => Triggered when a document already exists and has any value changed.
3. onDelete => Triggered when a document with data is deleted.
4. onWrite => Triggered when onCreate, onUpdate or onDelete is triggered.
Here we are using onCreate
that fires when we have a new entry in posts
collection.
That's it we have cloud function that fullfils our needs and took very less time to setup.
I hope you enjoyed it and learned something from this. I will be back with another new post untill then bye.
Top comments (0)