Created
January 22, 2015 09:12
-
-
Save alexzaporozhets/f33a6f802479ee85b17f to your computer and use it in GitHub Desktop.
RabbitMQ + rabbitjs
This file contains 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
{ | |
"name": "rabbit", | |
"engines": { | |
"node": "0.10.x", | |
"npm": "1.4.x" | |
}, | |
"dependencies": { | |
"rabbit.js": "~0.4.2" | |
} | |
} |
This file contains 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 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'); | |
}); | |
}); |
This file contains 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 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