Skip to content

Instantly share code, notes, and snippets.

@emilioriosvz
Created September 7, 2017 12:04
Show Gist options
  • Save emilioriosvz/3c1b8a6b1e3ab771ea44e35c079c9f5f to your computer and use it in GitHub Desktop.
Save emilioriosvz/3c1b8a6b1e3ab771ea44e35c079c9f5f to your computer and use it in GitHub Desktop.
rabbitmq async await
var amqp = require('amqplib')
var open = require('amqplib').connect('amqp://localhost');
const connect = (url = 'amqp://localhost') => {
return new Promise((resolve, reject) => {
amqp.connect(url)
.then(conn => resolve(conn))
.catch(err => reject(err))
})
}
const createChannel = conn => {
return new Promise((resolve, reject) => {
conn.createChannel()
.then(channel => resolve(channel))
.catch(err => reject(err))
})
}
const channelAssertQueue = (channel, queueName) => {
return new Promise((resolve, reject) => {
channel.assertQueue(queueName)
.then(asserted => resolve(channel))
.catch(err => reject(err))
})
}
const sendToQueue = (channel, queueName, buffer) => {
channel.sendToQueue(queueName, buffer)
}
const connection = async (queueName = 'msg.*') => {
var conn = await connect()
var channel = await createChannel(conn)
var assertedChannelToQueue = await channelAssertQueue(channel, queueName)
return channel
}
module.exports = connection
@julien-sarazin
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment