Created
October 23, 2014 22:10
-
-
Save james-huston/6f6c186d45c8153689ab to your computer and use it in GitHub Desktop.
nsqd simple example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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