Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to watch for sub-child added event in firebase

Tags:

firebase

is there a way to watch for grandchild_added event with firebase?

I've set up a observer for child_added using the node.js javascript library but it won't trigger for when a child of a child is added.

I'm setting member presence info and would like to know when users enter a room.

i.e.

members/room/uname

like image 767
MonkeyBonkey Avatar asked Jan 09 '23 16:01

MonkeyBonkey


2 Answers

In many cases using child_changed will be very cumbersome. It's a better approach to listen for child events itself.

Register a listener for the root, and register a listener for every child.

Here is the code in javascript to demonstrate this:

var Firebase = require("firebase");
var firebaseParentUrl = config.firebase.url
var firebaseChildUrl = config.firebase.url + "/child"

var parent = new Firebase(firebaseParentUrl, config.firebase.secret);

theParent.on("child_added", function (snapshot) {
    var id = snapshot.key()
    var theChild = new Firebase(firebaseChildUrl + id, config.firebase.secret)
    theChild.on("child_added", function (snapshot) {
        var grandChild = snapshot.val()
        //now do something with grandChild
    })
})
like image 162
Ankan-Zerob Avatar answered May 22 '23 10:05

Ankan-Zerob


Listen for child_changed. Adding a grandchild counts as changing a child.

This might not be as granular as you like, unfortunately, but this was the only thing that worked for me. (Probably listening for 'value' works too, but this is even less granular.)

like image 28
Amy M Avatar answered May 22 '23 09:05

Amy M