segmentio / nsq.js

NSQ client for nodejs
203 stars 34 forks source link

support Ready(count) #53

Open juliangruber opened 9 years ago

dweinstein commented 9 years ago

would be nice

dweinstein commented 9 years ago

err, maybe it's already there? (see this) just wasn't in the docs...

[edit] So I played w/ it a bit and there is a bit of a race condition, I guess--if you set the reader.on('message', ..) handler at same time as doing the reader.ready(x) you'll likely not get it set in time and messages will start. e.g.,:


reader.ready(0)
reader.on('messages', function(msg) {
... // this will still start accumulating
});

vs

reader.ready(0)
setTimeout(function() {
  reader.on('messages', function(msg) {
    ... // this will wait for reader.ready(n) n > 0
  });
}, 1000);

[edit 2] unless you config your reader by specifying ready: false ... so just don't forget to set that to false! Personally I'd probably have both false and 0 have the same behavior here?

juliangruber commented 9 years ago

Excellent research :) We could even have nsq.reader({ ready: 3 }), to set before connecting.

reader.ready(0) won't have the same behavior because it's called after the internal connection has been created, and the ready value will already have been set.

@tj was the Ready api not ready yet?

tj commented 9 years ago

TBH I forget, I think it initiates with a RDY so you can't stop it immediately since it's not sync but otherwise you can adjust