Realtime Database Triggers with Firebase Functions

Hilal Diab
Nov 14, 2020

--

Cloud Functions for Firebase is truly a great way to add quick database triggered events to your project with minimal effort and deploy time.
Warning: May cause addiction.

Cloud Functions for Firebase with logo
Cloud Functions for Firebase

If you still do not have Firebase Functions setup, please refer to the documentation found here to get started with this amazing tool.

Event types:

  1. Create: triggered the first time you write to a certain path
  2. Delete: triggered when you delete a certain path
  3. Update: triggered if you change the content of a certain path
  4. Write: all the above

1. Create:

Let’s say you want to update your coworkers/clients each time a new entry is added to /publishedDrafts a path that contains all the articles written by your users.

2. Delete:

Keep a backup of a deleted article just in case if the user regrets it later.

3. Update:

Compare the previous value with the new value of the published article.

4. Write:

This can handle all of the above cases.

5. Bonus:

You can check who wrote to a certain path:

context.authType === 'ADMIN'
context.authType === 'USER' -> context.auth.uid will contain the user Id

Don’t forget to deploy:

firebase deploy --only functions
Firebase Cloud Functions logo
Cloud Functions for Firebase

--

--

Hilal Diab

Co-Founder at Market Mapper, Love to mess with new technologies and building a business from the ground up. https://www.linkedin.com/in/hilal-diab-8a926877