Created
November 8, 2017 07:42
-
-
Save parthghiya/114a6e19208d0adca7bda6744c6de23e to your computer and use it in GitHub Desktop.
Sample Queue Implementation to achieve Asychronous Messaging communication
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 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