Wednesday, December 6, 2017

Firebase Realtime Database Trigger

if you want to know about firebase and firebase cloud functions before jumping into this article, please visit the following links.

1) Firebase RealTime Database
2) Firebase Authentication Service
3) Why we need cloud functions
4) Introduction to cloud functions

Realtime Database Trigger :

We can invoke realtime db trigger in response to any database events from the client.
From your web or mobile app when ever there is a database event, cloud realtime db trigger can be invoked. It is a post db operation.

For ex: User sends a msg called "stupid message" from the app to write into message node. If a db trigger is attached to the message node, it can listen to the onWrite() event, read the incoming msg, change to "Wonderful message" and stores back.

Please checkout the following projects from github if you want to play around.

Web Client:
1) https://github.com/fullstacktechnos/firebase-frontend

Firebase Functions:
2) https://github.com/fullstacktechnos/firebase-functions


** Following function from the web client will write a message to firebase realtime database having node "messages"

var ref = firebase.database().ref('messages');
                var data = {
                    type: 'Generic',
                    description: 'Stupid msg'
                }
                ref.push(data);



** Following cloud function in firebase will be invoked, sanitized the data and save it back.

//db trigger
function sanitize(msgDescription) {
  var sanitisedContent = msgDescription;
  sanitisedContent = sanitisedContent.replace(/\bstupid\b/ig, "wonderful");
  return sanitisedContent;
}
exports.sanitizeMessage = functions.database.ref('/messages/{mid}').onWrite(event => {
  const msg = event.data.val();

  //prevent calling same trigger again.
  if (msg.sanitized) {
    return;
  }
  console.log("Sanitizing new message : " + event.params.mid);
  console.log(msg);
  msg.sanitized = true;

  msg.description = sanitize(msg.description);
  return event.data.ref.set(msg);
})

*Please check the github project mention above for the working copy.

- The above db trigger (sanitizeMessage) will be invoked every time there is a new message write.
- It reads the incoming message after the message written into the db(as db triggers are post operation trigger), check the word "stupid" in it, if present change it to "wonderful" and writes back.

- If we write back to the same node again the same db trigger will be invoked which will continue for infinite loop. So to avoid the infinite looping we are checking at the beginning if the message is sanitized. If it is then we don't need to proceed.

- Its always better to return a promise from the trigger as mentioned in the last line of "sanitizeMessage" function. This way google function won't be killed until the promise is either resolved or rejected. So we can wait for the db operation to complete.

Event Handlers:
onWrite(), which triggers when data is created, destroyed, or changed in the Realtime Database.
onCreate(), which triggers when new data is created in the Realtime Database.
onUpdate(), which triggers when data is updated in the Realtime Database.
onDelete(), which triggers when data is deleted from the Realtime Database.

**Note:
For more information please visit:
https://firebase.google.com/docs/functions/database-events

1 comment:

  1. It has the continually far better gain your promises with the set off as said before within the last few to the line of Heroku Vs Aws performance. With this being performance aren't going to be harmed through to the promises is definitely frequently managed and also terminated. Hence we will bide time until a db business in order to complete.

    ReplyDelete