Created
May 2, 2022 13:06
-
-
Save dipanshuchaubey/15d1164dafca84d494b8c4bda33d366c to your computer and use it in GitHub Desktop.
RabbitMQ Pub-Sub
This file contains 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
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() |
This file contains 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
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