Firebase node.js push async

771 views
Skip to first unread message

Artem Alekseev

unread,
Aug 22, 2013, 8:46:59 AM8/22/13
to fireba...@googlegroups.com

Trying to use Firebase as a nodejs module. And i have a problem:

Firebase docs say to code for nodejs the same as for javascript Firebase library.

var newChildRef = myRootRef.push{{name: "Child1"}, function(error){
   if(!error){
     myModel.addChildto(newChildRef); // Here is calling global var inside a callback!   
   }
});

In nodejs we need to use async calls to databases. So the standard node way is:

 myRootRef.push{{name: "Child1"}, function(newChildRef,error){
       if(!error){
         myModel.addChildto(newChildRef); 
       }
    });

Please explain how should I code in node js using Firebase, not to spoil an async style.

wu...@firebase.com

unread,
Aug 24, 2013, 11:22:32 AM8/24/13
to fireba...@googlegroups.com
Hello Artem!

I'm a bit lost about what you mean by "spoil [node.js] async style". Is this actually a question on how to wrap the call to look like a node.js callback, or is this a criticism of the Firebase approach? Also, in your example you show node.js "standard" way as function(data, error) {...} but I'm most familiar with the error appearing first, as in function(error, data) { ... }.

Sorry to be so dense; sometimes I can be a bit too literal.

Nick Sivo

unread,
Aug 24, 2013, 2:52:48 PM8/24/13
to fireba...@googlegroups.com
My understanding is that current API forces you to use local closures
instead of a single handler method:

var myRootRef = ...;

function childAdded(error) {
if(!error) {
// wait, which child?
}
}

function addChild(name) {
myRootRef.push({name: name}, childAdded);
}

There's no way to make the above code work.

If, however, the callback function signature included the newly
created item, then this would work:

var myRootRef = ...;

function childAdded(error, newNodeRef) {
if(!error) {
// do something with newNodeRef
}
}

function addChild(name) {
myRootRef.push({name: name}, childAdded);
}

.NET's IAsyncResult works this way for this exact reason.

-Nick
> --
> You received this message because you are subscribed to the Google Groups
> "Firebase Google Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to firebase-tal...@googlegroups.com.
> To post to this group, send email to fireba...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Andrew Lee

unread,
Aug 25, 2013, 2:25:05 PM8/25/13
to fireba...@googlegroups.com
Artem / Nick -

Thanks for the feedback here! This is a weakness in our current API, and we have open cases to evaluate various solutions here. 

We'll update the list when we have something ready to show.

-andrew
--
Andrew Lee
Founder, Firebase
http://twitter.com/startupandrew

Artem Alekseev

unread,
Jan 21, 2014, 5:31:05 AM1/21/14
to fireba...@googlegroups.com
Any new regarding this issue since August?

Michael Wulf

unread,
Jan 21, 2014, 6:18:30 PM1/21/14
to fireba...@googlegroups.com
Artem,

I'm looking into this now. I'll update you and the list when I find out the status.

Hang in there!

Andrew Lee

unread,
Jan 22, 2014, 5:39:59 PM1/22/14
to fireba...@googlegroups.com
Hi Artem -

We haven't made an API change here yet. We're still planning to change things around a bit to make this sort of thing easier, but I actually don't have concrete details right now.

However, I'd actually suggest you change your code around a bit here. Here's what I'd recommend:

//Create a reference to a new piece of data.
var newChildRef = myRootRef.push();

//Write some data to that reference
newChildRef.set({name: "Child1"}, function(error){
   if(!error){
      //yay, the server got our data.
   }
});

//Do something with out local model... there's no reason to wait for the server since all Firebase operations work against a local cache.
myModel.addChildto(newChildRef);

I hope that's helpful!

-Andrew

Artem Alekseev

unread,
May 20, 2014, 6:13:32 AM5/20/14
to fireba...@googlegroups.com
var newChildRef = myRootRef.push();

This will block node server. So 1000 requests of the page with this code will make stop the server doing anything but making 1000 pushes. This is a blocking operation.

Michael Lehenbauer

unread,
May 20, 2014, 11:55:02 AM5/20/14
to fireba...@googlegroups.com
push() without args doesn't do any I/O.  So yes, .push() is "blocking", but only in the same way Math.floor() is blocking...

Try it for yourself...

var Firebase = require('firebase');

var myRootRef = new Firebase('https://test.firebaseio-demo.com');
var start = new Date().getTime();
for(var i = 0; i < 1000; i++) {
  var newChildRef = myRootRef.push();
}
console.log('Elapsed', new Date().getTime() - start);




For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages