Skip to content

Instantly share code, notes, and snippets.

@agusrichard
Created March 22, 2022 15:02
Show Gist options
  • Save agusrichard/a3098d88f33ccf0f39344f044c846f58 to your computer and use it in GitHub Desktop.
Save agusrichard/a3098d88f33ccf0f39344f044c846f58 to your computer and use it in GitHub Desktop.
import amqp, { Connection } from 'amqplib/callback_api'
const createMQProducer = (amqpUrl: string, queueName: string) => {
console.log('Connecting to RabbitMQ...')
let ch: any
amqp.connect(amqpUrl, (errorConnect: Error, connection: Connection) => {
if (errorConnect) {
console.log('Error connecting to RabbitMQ: ', errorConnect)
return
}
connection.createChannel((errorChannel, channel) => {
if (errorChannel) {
console.log('Error creating channel: ', errorChannel)
return
}
ch = channel
console.log('Connected to RabbitMQ')
})
})
return (msg: string) => {
console.log('Produce message to RabbitMQ...')
ch.sendToQueue(queueName, Buffer.from(msg))
}
}
export default createMQProducer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment