Skip to content

Instantly share code, notes, and snippets.

@james-huston
Created October 23, 2014 22:10
Show Gist options
  • Save james-huston/6f6c186d45c8153689ab to your computer and use it in GitHub Desktop.
Save james-huston/6f6c186d45c8153689ab to your computer and use it in GitHub Desktop.
nsqd simple example
var nsq = require('nsq.js');
var reader = nsq.reader({
// nsqlookupd: ['localhost:4161'],
nsqd: ['localhost:4150'],
topic: 'events',
channel: 'ingestion',
maxInFlight: 250
});
var stop = false;
var n = 0;
reader.on('message', function(msg){
++n;
msg.finish();
console.log('message', n);
});
console.log('reading');
var writer = nsq.writer({ port: 4150 });
function done(){
console.log(n);
writer.close(function () {
console.log('writer closed');
reader.close(function () {
console.log('reader closed');
process.exit(0);
});
});
}
function next() {
if (stop) return done();
setImmediate(function(){
console.log('writing');
writer.publish('events', ['foo', 'bar', 'baz'], next);
});
}
next();
setTimeout(function(){
stop = true;
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment