amqp.connect(URI) .then((connection) => { const donePromise = destroy ? Promise.all( queues.map(deleteQueue(connection)).concat(exchanges.map(deleteExchange(connection))) ) : Promise.all(queues.map(clearQueue(connection))); return donePromise .then(() => connection.close()) .catch(() => {}); })
initialize() { return amqp .connect('amqp://localhost') .then(conn => conn.createChannel()) .then(channel => { this.channel = channel; return this.channel.assertQueue(this.qName); }) .then(q => this.queue = q.queue) .catch(err => console.log(err.stack)) ; }
async connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }
constructor() { this.rabbitConfig = config.get('rabbitmq'); const rabbitMqUrl = `amqp://${this.rabbitConfig.user}:${this.rabbitConfig.password}@${this.rabbitConfig.host}`; logger.info('Starting RabbitMQ connection :', rabbitMqUrl); this.handler = amqplib.connect(rabbitMqUrl, { keepAlive: true }); }
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'myqueue'; var msg = 'hello world 1'; ch.assertQueue(q, {durable: false}); // Note: on Node 6 Buffer.from(msg) should be used ch.sendToQueue(q, Buffer.from(msg)); console.log(" [x] Sent %s", msg); }); setTimeout(function() { conn.close(); process.exit(0) }, 500); });
async function init() { const conn = await amqp.connect('amqp://localhost'); process.once('SIGINT', conn.close.bind(conn)); ch = await conn.createConfirmChannel(); logger.info('client has connected to mq'); await ch.assertQueue(queueName, { durable: false }); }
amqp .connect('amqp://localhost') .then(conn => { connection = conn; return conn.createChannel(); }) .then(ch => { channel = ch; produce(); }) .catch(err => console.log(err)) ;
async function connect() { const connection = await amqp.connect('amqp://localhost:5672'); const channel = await connection.createChannel(); const queue = channel.assertQueue(QUEUES.JOBS); channel.consume(QUEUES.JOBS, (job) => { let data = JSON.parse(job.content); console.log(data); channel.ack(job); }); console.log('Waiting for the message!'); }
const produce = async (queue, message, durable = false, persistent = false) => { const connect = await amqp.connect(config.rabbitMqUrl) const channel = await connect.createChannel() await channel.assertQueue(queue, { durable }) await channel.sendToQueue(queue, Buffer.from(message), { persistent }) console.log('Message produced: ', queue, message) }
const createConnection = async () => { try { return await amqp.connect(url) } catch (err) { throw err } }
amqp.connect(host) .then(connection => connection.createConfirmChannel()) .then((channel) => { channels[host] = channel[host] ? channels[host] : {}; channels[host][context] = channel; return channels[host][context]; }) .catch((error) => { helpers.log('error', error.message, options); return error; })
const listen = async () => { // connect to RabbitMQ let connection = await amqp.connect(messageQueueConnection) // create a channel and prefetch one message at a time let channel = await connection.createChannel() await channel.prefetch(1) // start consuming messages await consume({ connection, channel }) }
// Me conecto al server. amqp.connect('amqp://localhost', (err, conn)=>{ if (err) console.log('error al conectar',err); else conn.createChannel(createChannel); });
async connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }
// Me conecto al server de colas, si lo logro creo un canal. amqp.connect('amqp://localhost', (err, conn)=>{ //Si hubo un error. if (err) console.log('Hubo un error al conectar al server rabbit',err); else conn.createChannel(createChannel); });