Skip to content

Instantly share code, notes, and snippets.

@RafaelGSS
Last active September 1, 2019 23:54
Show Gist options
  • Save RafaelGSS/5ed685908096377609f6f377b077ad0c to your computer and use it in GitHub Desktop.
Save RafaelGSS/5ed685908096377609f6f377b077ad0c to your computer and use it in GitHub Desktop.
Communication between microservices - RabbitMQ - CW Pattern - consumer
const amqp = require('amqplib')
async function createConnection (uri = 'guest:guest@localhost:5672') {
const connection = await amqp.connect('amqp://' + uri)
return connection
}
createConnection()
.then(conn => conn.createChannel())
.then(ch => {
console.log('Channel created!')
const queue = 'messages'
ch.assertQueue(queue)
ch.consume(queue, function (msg) {
if (msg !== null) {
console.log('%s Received: %s', new Date(), msg.content.toString())
ch.ack(msg)
}
})
});
// Consumer channel without prefetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment