I am using the firebase library and it works nicely until I try to listen for database changes. The only success I have is listening for specific key/value pairs on a node. I am not able to see change events for children of a specified node.
When I am able to see the changes to a specific key/value the function returns a null path and null data so it is still not very useful.
To test I simply setup a copy of the documentation example.
Below is an example of how to set up a stream and a listener. This listener callback has both path and data when updates are made to Firebase.
const FIREBASE_NAME = "<YOUR FB NAME HERE>";
const FIREBASE_AUTH_KEY = "<YOUR AUTH KEY HERE>";
server.log("AGENT RUNNING");
firebase <- Firebase(FIREBASE_NAME, FIREBASE_AUTH_KEY);
tempData <- {"temp": 24.341, "humid" : 33.2};
// Clear data node, so we always start test with the same empty node
firebase.write("/data", {}, function(err, res) {
server.log(err == null);
server.log(res == null); // Expected: true
})
// Wait for node to be cleared
imp.wakeup(1, function() {
// Open a stream on the root directory, this allows you to open a listener on anything in the database
firebase.stream();
// Open a listener on the data node
firebase.on("/data", function(path, data) {
// Only do something if we have data
if (data != null) {
// Log info
server.log("in listener, we have new data...");
server.log("Data changed in path: " + path);
server.log("New data: " + data);
}
})
// Function that writes to data node, so we can test the listener
function writeData() {
// Change data values
tempData.temp += 1;
tempData.humid += 1;
// Write the updated data to Firebase
firebase.push("/data/temp", tempData.temp, null, function(err, res) {
server.log(http.jsonencode(res));
})
firebase.push("data/humid", tempData.humid, null, function(err, res) {
server.log(http.jsonencode(res));
})
}
// Set a couple timers to write data to Firebase
imp.wakeup(1, writeData);
imp.wakeup(3, writeData);
imp.wakeup(5, writeData);
})
// Close the stream
imp.wakeup(10, function() {
firebase.closeStream();
})
Produces logs
2017-08-10 12:15:43 -07:00 [Status] Agent restarted: reload.
2017-08-10 12:15:43 -07:00 [Agent] AGENT RUNNING
2017-08-10 12:15:43 -07:00 [Agent] true
2017-08-10 12:15:43 -07:00 [Agent] true
2017-08-10 12:15:43 -07:00 [Status] Device connected
2017-08-10 12:15:45 -07:00 [Agent] { “name”: “-KrCZj1LuNFhXSOnhGAJ” }
2017-08-10 12:15:45 -07:00 [Agent] { “name”: “-KrCZj1P8JRYSK0VS73B” }
2017-08-10 12:15:45 -07:00 [Agent] in listener, we have new data…
2017-08-10 12:15:45 -07:00 [Agent] Data changed in path: /data/humid/-KrCZj1LuNFhXSOnhGAJ
2017-08-10 12:15:45 -07:00 [Agent] New data: 34.2
2017-08-10 12:15:45 -07:00 [Agent] in listener, we have new data…
2017-08-10 12:15:45 -07:00 [Agent] Data changed in path: /data/temp/-KrCZj1P8JRYSK0VS73B
2017-08-10 12:15:45 -07:00 [Agent] New data: 25.341
2017-08-10 12:15:47 -07:00 [Agent] { “name”: “-KrCZjUG7seT8-hMbCRl” }
2017-08-10 12:15:47 -07:00 [Agent] { “name”: “-KrCZjUHCs_I4n0A9CZc” }
2017-08-10 12:15:47 -07:00 [Agent] in listener, we have new data…
2017-08-10 12:15:47 -07:00 [Agent] Data changed in path: /data/temp/-KrCZjUG7seT8-hMbCRl
2017-08-10 12:15:47 -07:00 [Agent] New data: 26.341
2017-08-10 12:15:47 -07:00 [Agent] in listener, we have new data…
2017-08-10 12:15:47 -07:00 [Agent] Data changed in path: /data/humid/-KrCZjUHCs_I4n0A9CZc
2017-08-10 12:15:47 -07:00 [Agent] New data: 35.2
2017-08-10 12:15:49 -07:00 [Agent] { “name”: “-KrCZjyWjf0aJmr7pNTv” }
2017-08-10 12:15:49 -07:00 [Agent] { “name”: “-KrCZjyWjf0aJmr7pNTw” }
2017-08-10 12:15:49 -07:00 [Agent] in listener, we have new data…
2017-08-10 12:15:49 -07:00 [Agent] Data changed in path: /data/humid/-KrCZjyWjf0aJmr7pNTv
2017-08-10 12:15:49 -07:00 [Agent] New data: 36.2
2017-08-10 12:15:49 -07:00 [Agent] in listener, we have new data…
2017-08-10 12:15:49 -07:00 [Agent] Data changed in path: /data/temp/-KrCZjyWjf0aJmr7pNTw
2017-08-10 12:15:49 -07:00 [Agent] New data: 27.341