Use amqplib npm module to use a rabbitmq integration on nodejs.
I give sample publish consume code on rabbitmq.
Publish code:
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://username:password@localhost:5672', function(err, connection) {
if (err) throw err;
connection.createChannel(function(err, channel) {
if (err) throw err;
var exchange = 'data';
var msg = "Welcome to Node and rabbitMQ";
channel.assertExchange(exchange, 'fanout', {
durable: false
})
channel.publish(exchange, '', Buffer.from(msg));
console.log(msg)
})
})
Consume Code
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://username:password@localhost:5672', function(err0, connection) {
if (err0) throw err0;
connection.createChannel(function(err1, channel) {
if (err1) throw err1;
var exchange = 'data';
channel.assertExchange(exchange, 'fanout', { durable: false })
channel.assertQueue('', {
exclusive: true
}, function(err2, value) {
channel.bindQueue(value.queue, exchange, '');
channel.consume(value.queue, function(msg) {
console.log("message:", msg.content.toString())
}, { noAck: true })
})
})
})