/** * Subscribe to balanced action commands * * @param {String} action * @memberof AmqpTransporter */ subscribeBalancedRequest(action) { const queue = `${this.prefix}.${PACKET_REQUEST}B.${action}`; return this.channel.assertQueue(queue, this._getQueueOptions(PACKET_REQUEST, true)) .then(() => this.channel.consume( queue, this._consumeCB(PACKET_REQUEST, true), this.opts.consumeOptions )); }
async function init() { const connection = await amqp.connect(config.rabbitmq.url) const e = config.rabbitmq.exchange const channel = await connection.createChannel() await channel.assertExchange(e, 'fanout', { durable: false, autoDelete: true }) const q = await channel.assertQueue('', { exclusive: true }) await channel.bindQueue(q.queue, e, '') channel.consume(q.queue, (msg) => { console.log(msg.content.toString()) channel.ack(msg) }) }
_listenForResponses() { return this.channel.consume(this.replyQueue, msg => { const correlationId = msg.properties.correlationId; const handler = this.idToCallbackMap[correlationId]; if (handler) { handler(JSON.parse(msg.content.toString())); } }, {noAck: true}); }
async function consMsg() { try{ console.info('consumer listening...!') const chan = await initMQ() await chan.consume(queueCfg.queueId, function(msg){ console.log(msg.content.toString()) chan.ack(msg) }, { noAck: false }) } catch(e) { console.log(String(e)) } }
subscribeQueue(queue, messageHandler) { return this.handler .then(conn => conn.createChannel()) .then(channel => { logger.info(`Consuming from ${queue}`); return channel.assertQueue(queue, { exclusive: false }) .then(() => channel.consume(queue, message => messageHandler(message.content.toString()), { noAck: true })) .then(() => logger.info(`Consumed from ${queue}`)) }) .catch(logger.error); }
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'myqueue'; ch.assertQueue(q, {durable: false}); console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q); ch.consume(q, function(msg) { console.log(" [x] Received %s", msg.content.toString()); }, {noAck: true}); }); });
async start (doForEveryItem) { this.channel.consume(this.queueName, (msg) => { doForEveryItem(msg) this.channel.ack(msg) }) }
async function init() { const connection = await amqp.connect(config.rabbitmq.url) const q = config.rabbitmq.channel const channel = await connection.createChannel() await channel.assertQueue(q, { autoDelete: false }) channel.prefetch(1) channel.consume(q, (msg) => { console.log(msg.content.toString()) channel.ack(msg) }) }
amqp.connect('amqp://localhost:5672', function (err, conn) { conn.createChannel(function (err, ch) { const q = 'hello'; ch.assertQueue(q, { durable: false }); ch.prefetch(1); console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q); ch.consume(q, function (msg) { console.log(" [x] Received %s", msg.content.toString()); }, { noAck: true }); }); });
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!'); }
amqp.connect('amqp://localhost:5672', function (err, conn) { conn.createChannel(function (err, ch) { ch.assertQueue(queue, { durable: false }); ch.prefetch(1); console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue); ch.consume(queue, async function (msg) { const { name, email } = JSON.parse(msg.content); await User.create({ name, email }); console.log(" [x] Received %s", msg.content.toString()); }, { noAck: true }); }); });
/** * Subscribe to balanced event command * * @param {String} event * @param {String} group * @memberof AmqpTransporter */ subscribeBalancedEvent(event, group) { const queue = `${this.prefix}.${PACKET_EVENT}B.${group}.${event}`; return this.channel.assertQueue(queue, this._getQueueOptions(PACKET_EVENT + "LB", true)) .then(() => this.channel.consume( queue, this._consumeCB(PACKET_EVENT, true), this.opts.consumeOptions )); }
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; ch.assertQueue(q, {durable: false}); console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q); ch.consume(q, function(msg) { console.log(" [x] Received %s", msg.content.toString()); }, {noAck: true}); }); });
async start (doForEveryItem) { this.channel.consume(this.queueName, (msg) => { doForEveryItem(msg) this.channel.ack(msg) }) }