I am new to Firebase and building a prototype to test if suitable for our needs. I\'ve got a chat example up and running, so far so good.
Next is the need to listen for
In order to receive events, you need to listen into 'Firebase' provided events which are put, patch, keep-alive etc... I have tried and it works.
You can refer following code snippet,
if (typeof (EventSource) !== "undefined") {
var source = new EventSource("https://xxxxxxxxx.firebaseio.com/TestNode.json");
source.onmessage = function (event) {
document.getElementById("result").innerHTML += event.data + "
";
};
source.addEventListener("message", function (e) {
console.log(e.data);
}, false);
source.addEventListener("open", function (e) {
console.log("Connection was opened.");
}, false);
source.addEventListener("error", function (e) {
console.log("Error - connection was lost.");
}, false);
//magic goes here!
source.addEventListener("patch", function (e) {
console.log("Patch UP - " + e.data);
}, false);
//And here!
source.addEventListener("put", function (e) {
console.log("Put UP - " + e.data);
}, false);
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
Refer following documents, https://firebase.google.com/docs/database/rest/retrieve-data
https://firebase.google.com/docs/reference/rest/database/