Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do event-driven on Firebase

Our startup is "cloud-native" using Firebase and Google Cloud. We are looking into going into event-driven design, but I have a hard time matching the concept to the specific services on Firebase or GCP.

An example: A user creates a contract draft (a Firestore Document) via a mobile app. We need to trigger the following operations:

  • Notify other users that are parties in the contract via email and push notification
  • Write to the notifications table so that the user could have an "inbox" in the app and mark them read or deleted
  • Generate a job that will poll the contract in a month, and if it does not change state, delete the contract
  • Run a series of calculations on the server

We managed to put those actions into Cloud Function triggers so far, but our trigger code has become messy, and in rare cases, the operations run above the Cloud Function thresholds (2GB and 9 minutes).

Our plan to improve the code base and reduce trigger-based operations:

  1. A user is creating a contract (Cloud Firestore)
  2. The Cloud Firestore trigger creates an event (Cloud Functions)
  3. Services that need to do something about it subscribe to the event stream and execute appropriate actions. (What comes here?)

I've investigated Pub/Sub but thought it would be an overkill, since we don't have to deal too much with services outside of GCP, and our scale does not need it atm. Can we achieve the above within the realm of Firebase tools?

like image 434
Dmitri Borohhov Avatar asked Oct 27 '25 03:10

Dmitri Borohhov


2 Answers

Pubsub is actually exactly what you need. It's not overkill - it's a common and preferred solution in Google Cloud to handle subscriptions to events. In fact, Cloud Functions is already built on top of pubsub.

The Firebase tools actually provide their own interface for deploying pubsub Cloud Functions. You can read about that in the documentation. However, if you need more computing power than provided by Cloud Functions, Firebase products will not help you, and you will need to look into Google Cloud products like Compute Engine. You will still likely use pubsub to notify your chosen backend.

like image 67
Doug Stevenson Avatar answered Oct 29 '25 18:10

Doug Stevenson


Since the scale is not an issue for you at the moment, you can achieve these using the Firebase tools.

If you did not read already the Cloud Firestore triggers documentation would be a good read in order to understand how to improve and make them more future proof in case of different events.

Also, it would be worth checking this documentation and look into Observables if you want to create subscriptions with services that would look for certain actions to be executed.

like image 27
Stefan Neacsu Avatar answered Oct 29 '25 17:10

Stefan Neacsu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!