Lessons Learned — Cloud Functions for Firebase

Ron Nagar
Blog | The Elegant Monkeys
2 min readAug 3, 2017

--

For the last month, I have been using Cloud Functions for Firebase for a project on a tight schedule. Here is why I think Cloud Functions for Firebase was the right choice.

  • Easy to learn — The API is very simple and provides everything to mange your Firebase app.
  • Bootstrap your server side development —Cloud Functions for Firebase manages the infrastructure for you, so the only thing left is to write your business logic without any reference to the environment (scale, users, etc…).
  • Function types — The functions can be triggered by: Realtime database changes, authentication, analytics, Cloud Storage, Cloud Pub/Sub and HTTP trigger (can use all express middlewares). These triggers cover everything that is needed for your Firebase app including integrations to other cloud services.
  • Easy to monitor — Firebase provides statistics & logs for every function, as well as integration with Google Stackdriver for complex monitoring.
  • Easy to deploy — Deploy Cloud Functions for Firebase is very simple. You only need to run the deploy command with firebase-tools and your functions are good to go, firebase-tools supports continues integration so you can deploy functions using CI pipeline for full automation.

There are some things you need to be aware of when you build Cloud Functions for Firebase. This list is the result of blood, sweat and tears so please read carefully.

  • Ensure promise is returned — When running async function, you MUST return a promise, if not your function will throw a timeout exception and will become a “ghost” function (very hard to get rid off!).
  • Choose the right trigger—Function can be triggered by a very specific event (new data created, data deleted, etc) or a more general event (like data written, etc). Choose the specific event when it is possible even if you need to split to more functions. At the beginning of using Cloud Functions for Firebase I wrote functions with triggered by general events and the code became long, complex and the function was fired in situations I was not intending to. After I have refactored and split my functions to listen to specific event they become clearer and simpler.
  • Listen to events of the most accurate path— When building a function based on a Realtime database trigger make sure to choose the exact path. This function will be triggered by any change which is made to a property or a child of this path. Listening to a path of a nested object can trigger your function more than needed.

I really think Cloud Functions for Firebase is great for quick & agile development of server side on Firebase. It feels like this is the last of piece of the puzzle that Firebase needed.

--

--