Created
March 22, 2022 15:02
-
-
Save agusrichard/a3098d88f33ccf0f39344f044c846f58 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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