Skip to content

Instantly share code, notes, and snippets.

@carvalhoviniciusluiz
Last active February 19, 2020 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carvalhoviniciusluiz/43120aacfe289308768938a4ce3800f3 to your computer and use it in GitHub Desktop.
Save carvalhoviniciusluiz/43120aacfe289308768938a4ce3800f3 to your computer and use it in GitHub Desktop.
Send sync queue
const amqp = require('amqplib');
const uuid = require('uuid');
module.exports = {
async sendPublish (req, res) {
const url = ``;
const exchange = '';
const routing_key = '';
const conn = await amqp.connect(url);
const channel = await conn.createChannel();
await channel.assertExchange(exchange, 'direct', { durable: true });
const q = await channel.assertQueue('', { exclusive: true });
await channel.publish(
exchange,
routing_key,
Buffer.from(
JSON.stringify({
chave: 'provident',
cnpjCertificado: '54.117.372/0001-21',
idUsuario: '1',
})
),
{
contentType: 'application/json',
correlationId: uuid.v4(),
replyTo: q.queue,
}
);
let content = null;
await channel.consume(
q.queue,
async data => {
({ content } = data);
await channel.close();
await conn.close();
},
{ noAck: true }
);
const result = content ? JSON.parse(content.toString()) : {};
return res.status(202).json(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment