.createChannel() .then((channel) => { this.channel = channel;
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'; 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 connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }
sendMessage(exchange, key, message) { return this.handler .then(connection => connection.createChannel()) .then(channel => channel.assertExchange(exchange, 'topic', { durable: true }) .then(() => channel.publish(exchange, key, new Buffer(JSON.stringify(message)))) .then(() => logger.debug('Sent message :', message)) .then(() => channel.close()) ) .catch(logger.error); }
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) }
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') .then(conn => { connection = conn; return conn.createChannel(); }) .then(ch => { channel = ch; produce(); }) .catch(err => console.log(err)) ;
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 }) }
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); }
// Me conecto al server. amqp.connect('amqp://localhost', (err, conn)=>{ if (err) console.log('error al conectar',err); else conn.createChannel(createChannel); });
amqp.connect(url, (err, conn) => { if (err) { throw err } conn.createChannel((err, channel) => { const queueName = 'queueUser' channel.assertQueue(queueName, { durable: true }) console.log('wait message....') channel.consume(queueName, message => { console.log('message: ', message.content.toString()) }, { noAck: true }) }) })
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) }
sendMessage(exchange, key, message) { return this.handler .then(connection => connection.createChannel()) .then(channel => channel.assertExchange(exchange, 'topic', { durable: true }) .then(() => channel.publish(exchange, key, new Buffer(JSON.stringify(message)))) .then(() => logger.debug('Sent message :', message)) .then(() => channel.close()) ) .catch(logger.error); }
async connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }