Skip to content

Instantly share code, notes, and snippets.

@dipanshuchaubey
Created May 2, 2022 13:06
Show Gist options
  • Save dipanshuchaubey/15d1164dafca84d494b8c4bda33d366c to your computer and use it in GitHub Desktop.
Save dipanshuchaubey/15d1164dafca84d494b8c4bda33d366c to your computer and use it in GitHub Desktop.
RabbitMQ Pub-Sub
const amqp = require('amqplib')
const connect = async () => {
try {
const connection = await amqp.connect('amqp://localhost:5672')
const channel = await connection.createChannel()
const result = await channel.assertQueue('jobs')
channel.consume('jobs', (message) => {
const data = JSON.parse(message.content.toString())
console.log(data.number)
if (data.number == 10) {
console.log(`Consumed ${data.number}`)
channel.ack(message)
}
})
return console.log('Waiting for Messages')
} catch (err) {
console.log(err)
}
}
connect()
const amqp = require('amqplib')
const message = { number: process.argv[2] }
async function connect() {
try {
const connection = await amqp.connect('amqp://localhost:5672')
const channel = await connection.createChannel()
const result = await channel.assertQueue('jobs')
channel.sendToQueue('jobs', Buffer.from(JSON.stringify(message)))
return console.log('Job Sent Successfully')
} catch (err) {
console.log(err)
}
}
connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment