Gaurang Sondagar
Gaurang Sondagar

Reputation: 311

How to use firebase realtime database without node.js

I'm newbie for Firebase Realtime Database , i want to implement firebase realtime database in my project but want to do it without using node.js. is it possible ? please help me sort out this problem.

and if it's possible to use firebase realtime database without using node.js then how can i proceed further. I have also placed my sample code below. please give me some suggestion.

var config = {
apiKey: "AIzaSyAEERy5Jbnns9CUurD7NwmOXXXXXXXXXX",
authDomain: "testing-XXXXX-db.firebaseapp.com",
databaseURL: "https://testing-XXXXX-db.firebaseio.com",
projectId: "testing-XXXXXX-db",
storageBucket: "testing-XXXXX-db.appspot.com",
messagingSenderId: "25861013XXXX"
};
firebase.initializeApp(config);

// Get a reference to the database service
var database = firebase.database();

i have successfully initialized it and i'm also getting results from cloud database.

firebase.database().ref("/chat").orderByChild("name").on("value", function(snapshot) {
      snapshot.forEach(function(childSnapshot) {

    // key
    var key = childSnapshot.key;
    // value, could be object
    var childData = childSnapshot.val();

    console.log(childData);
    $("#history").append('<div class="container"><p>'+childData.name+':'+childData.message+'</p><span class="time"> Time : '+childData.time+' </span></div>');
  });
});

problem is coming when i want to use database trigger for live updates for chat application.

i want to use following events/function.

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.

Any help is really appreciated. Apologies for bad grammar/language. Please don't give negative reviews as i'm really stuck at this.

Upvotes: 2

Views: 3233

Answers (1)

Alex MacArthur
Alex MacArthur

Reputation: 2286

I haven't the most experience with Firebase, but from my understanding, those functions you listed trigger by use of Cloud Functions, which aren't needed to respond to changes in the browser.

In the browser, to respond to a child change, use child_changed:

 firebase.database().ref("chat").orderByChild("name").on("child_changed", function(snapshot) {
     //-- Whatever.
 });

To respond to a child addition, use child_added:

 firebase.database().ref("chat").orderByChild("name").on("child_added",      function(snapshot) {
     //-- Whatever.
 });

To respond to to a child removal, use child_removed:

 firebase.database().ref("chat").orderByChild("name").on("child_removed", function(snapshot) {
          //-- Whatever.
 });

Also, note that if you're using .on("value") for anything, that will return a new data set each time that data changes in any way, which means that this will probably return the entiiiiiiiiiiiire chat every time a new message is sent, instead of just returning the new message. Might not want to do that:

 firebase.database().ref("chat").orderByChild("name").on("value", function (){});

If you just want to get that chat's data once, use .once("value").

I hope this helps?

Upvotes: 4

Related Questions