Skip to content

Instantly share code, notes, and snippets.

@alexzaporozhets
Created January 22, 2015 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexzaporozhets/f33a6f802479ee85b17f to your computer and use it in GitHub Desktop.
Save alexzaporozhets/f33a6f802479ee85b17f to your computer and use it in GitHub Desktop.
RabbitMQ + rabbitjs
{
"name": "rabbit",
"engines": {
"node": "0.10.x",
"npm": "1.4.x"
},
"dependencies": {
"rabbit.js": "~0.4.2"
}
}
var context = require('rabbit.js').createContext('amqp://..');
context.on('ready', function () {
// PUSH socket will send each message to a single connection, using round-robin
var pub = context.socket('PUSH');
pub.connect('events', function () {
pub.write(JSON.stringify({welcome: 'rabbit.js'}), 'utf8');
});
});
var context = require('rabbit.js').createContext('amqp://...');
context.on('ready', function () {
// WORKER socket is similar to a PULL socket, but requires that you call #ack on it to acknowledge
// that you have processed each message. Any messages left unacknowledged when the socket closes, or crashes,
// will be requeued and delivered to another connected socket (should there be one)
var worker = context.socket('WORKER', {prefetch: 1});
worker.connect('events', function(){
console.log('listening for events...');
});
worker.on('data', function(job) {
console.log(JSON.parse(job.toString()));
// A worker socket is read-only, and has the additional method #ack which acknowledges
// the oldest unacknowledged message, and must be called once only for each message
worker.ack();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment