Skip to content

Instantly share code, notes, and snippets.

@Marxpark
Last active May 25, 2020 15:03
Show Gist options
  • Save Marxpark/574c1b1fb4922efc8f9114c82d71dc9d to your computer and use it in GitHub Desktop.
Save Marxpark/574c1b1fb4922efc8f9114c82d71dc9d to your computer and use it in GitHub Desktop.
connecting msUser to Rabbit
var rabbit = require('amqplib/callback_api');
const pino = require('pino');
require('dotenv').config();
const LOGGER = pino({ level: process.env.LOG_LEVEL || 'info' });
LOGGER.info(`Connecting to RabbitMQ`)
rabbit.connect('amqp://localhost', (error0, connection) => {
if (error0) {
throw error0;
}
LOGGER.info("Creating default channel on default exchange")
connection.createChannel((error, channel) => {
if (error) {
throw error;
}
rabbit.channel = channel
channel.assertQueue("userLogin", {
durable: false
})
channel.assertQueue("frontendMessage", {
durable: false
})
channel.send = (queue, message) => {
channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)))
}
LOGGER.info("Attaching consumers...")
channel.consume("userLogin", (event) => {
console.log(JSON.parse(event.content.toString()))
}, {
noAck: true
})
LOGGER.info("All consumers ready")
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment