Skip to content

Instantly share code, notes, and snippets.

@parthghiya
Created November 8, 2017 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save parthghiya/114a6e19208d0adca7bda6744c6de23e to your computer and use it in GitHub Desktop.
Save parthghiya/114a6e19208d0adca7bda6744c6de23e to your computer and use it in GitHub Desktop.
Sample Queue Implementation to achieve Asychronous Messaging communication
var q = 'tasks';
var open = require('amqplib').connect('amqp://localhost');
// Publisher
open.then(function(conn) {
return conn.createChannel();
}).then(function(ch) {
return ch.assertQueue(q).then(function(ok) {
return ch.sendToQueue(q, new Buffer('something to do')); //Note : queue is filled up here
});
}).catch(console.warn);
// Consumer
open.then(function(conn) {
return conn.createChannel();
}).then(function(ch) { //Note : Review queue and take actions accordingly
return ch.assertQueue(q).then(function(ok) {
return ch.consume(q, function(msg) {
if (msg !== null) {
console.log(msg.content.toString());
ch.ack(msg);
}
});
});
}).catch(console.warn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment