Monday, February 5, 2018

Cloud Functions for Firebase

Cloud functions for firebase allow one to automatically run backend code in response to events triggered by firebase features and HTTPS requests. The code is stored in Google cloud and runs in managed environment. 



The cloud functions that a developer write can respond to events generated by below firebase and Google cloud features



1) Cloud Firestore Triggers

2) Realtime database triggers

3) Firebase authentication triggers 

4) Google analytics for firebase triggers 

5) Crashlytics Triggers 

6) Cloud Storage triggers 

7) Cloud Pub/Sub triggers 

8) HTTP triggers 



How does it work? 



After a function is deployed, Google servers begin to manage functions immediately. listening for events and running the function when it is triggered. As the load increases or decreases, Google responds by rapidly scaling the number of virtual server instances needed to run your function.



LifeCycle of a function 



- The developer writes code for a new function, selecting an event provider (such as Realtime Database), and defining the conditions under which the function should execute.

- The developer deploys the function, and Firebase connects it to the selected event provider.

-  When the event provider generates an event that matches the function's conditions, the code is invoked.

- If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.

- When the developer updates the function by deploying updated code, all instances for the old version are cleaned up and replaced by new instances.

- When a developer deletes the function, all instances are cleaned up, and the connection between the function and the event provider is removed.



To enable cloud functions, below is what has to be done: 



- Setup : Install CLI and initialise cloud functions in firebase project

- Write Functions: Write Javascript code (or Typescript ) to handle events from firebase services, Google cloud services, or other event providers. 

- Deploy and monitor : Deploy functions using firebase CLI. 



references:

https://firebase.google.com/docs/functions/

No comments:

Post a Comment