Reputation: 726
RouteChat example which is a bi-directional rpc, seems to be buffering messages on write. Is there a way to force writes to the outgoing stream immediately without buffering?
Upvotes: 1
Views: 1162
Reputation: 20232
The reason you're seeing this buffering behavior is because writes are actually completed asynchronously, so the server code needs to regularly allow other asynchronous operations to happen between writes to ensure that they are sent as soon as they are computed. You can take advantage of the optional callback argument to write
to send each message immediately after the last one is sent. The code in that example can instead be written using async.each
:
async.each(notes, function(note, next) {
console.log('Sending message "' + note.message + '" at ' +
note.location.latitude + ', ' + note.location.longitude);
var noteMsg = new messages.RouteNote();
noteMsg.setMessage(note.message);
var location = new messages.Point();
location.setLatitude(note.location.latitude);
location.setLongitude(note.location.longitude);
noteMsg.setLocation(location);
// Note the use of the next callback here
call.write(noteMsg, next);
}, function() {
// End the stream after all messages have been sent
call.end();
});
Upvotes: 1