/** * 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 )); }
/** * Send data buffer. * * @param {String} topic * @param {Buffer} data * @param {Object} meta * * @returns {Promise} */ send(topic, data, { balanced, packet }) { /* istanbul ignore next*/ if (!this.channel) return this.broker.Promise.resolve(); if (packet.target != null || balanced) { this.channel.sendToQueue(topic, data, this.opts.messageOptions); } else { this.channel.publish(topic, "", data, this.opts.messageOptions); } return this.broker.Promise.resolve(); }
/** * Disconnect from an AMQP server * * @memberof AmqpTransporter * @description Close the connection and unbind this node's queues. * This prevents messages from being broadcasted to a dead node. * Note: Some methods of ending a node process don't allow disconnect to fire, meaning that * some dead nodes could still receive published packets. * Queues and Exchanges are not be deleted since they could contain important messages. */ disconnect() { this.connectionCount = 0; if (this.connection && this.channel && this.bindings) { return this.broker.Promise.all(this.bindings.map(binding => this.channel.unbindQueue(...binding))) .then(() => { this.channelDisconnecting = this.transit.disconnecting; this.connectionDisconnecting = this.transit.disconnecting; }) .then(() => this.channel.close()) .then(() => this.connection.close()) .then(() => { this.bindings = []; this.channel = null; this.connection = null; }) .catch(err => this.logger.warn(err)); } }
const needAck = [PACKET_REQUEST].indexOf(cmd) !== -1; return this.channel.assertQueue(topic, this._getQueueOptions(cmd)) .then(() => this.channel.consume( topic, this._consumeCB(cmd, needAck), this.channel.assertExchange(topic, "fanout", this.opts.exchangeOptions), this.channel.assertQueue(queueName, this._getQueueOptions(cmd)), ]) .then(() => this.broker.Promise.all([ this.channel.bindQueue(...bindingArgs), this.channel.consume( queueName, this._consumeCB(cmd),
this.logger.info("AMQP channel is created."); channel.prefetch(this.opts.prefetch); .on("close", () => { this.channel = null; this.logger.info("AMQP channel is closed gracefully."); }) .on("error", (err) => { .on("drain", () => { this.logger.info("AMQP channel is drained."); }) .on("return", (msg) => { this.logger.warn("AMQP channel returned a message.", msg); });
const publish = async (exchangeName, exchangeType, message) => { const connect = await amqp.connect(config.rabbitMqUrl) const channel = await connect.createChannel() await channel.assertExchange(exchangeName, exchangeType, {durable: false}) await channel.publish(exchangeName, '', Buffer.from(message)) console.log('Message published: ', exchangeName, message) }
_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}); }
request(queue, message, callback) { const id = uuid.v4(); this.idToCallbackMap[id] = callback; this.channel.sendToQueue(queue, new Buffer(JSON.stringify(message)), {correlationId: id, replyTo: this.replyQueue} ); }
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)) ; }
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 connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }
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 }); }); });
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; var msg = 'Hello World!'; ch.assertQueue(q, {durable: false}); // Note: on Node 6 Buffer.from(msg) should be used ch.sendToQueue(q, new Buffer(msg)); console.log(" [x] Sent %s", msg); }); setTimeout(function() { conn.close(); process.exit(0) }, 500); });
/** * 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 )); }