Reputation: 6684
I have built an API endpoint to send mails based on Express. I currently call it using Axios when it is required making a post request directly to the route defined. So far so good.
Now I am trying to put RabbitMQ in the middle of this process so that I can disentangle the request and the response: once the message is queued, you don't need to wait for the message to be sent (I expect rather high volumes of messages to be sent).
Based on my understanding of how it works, I need to replace the direct call to the mailer API with a call to RabbitMQ to add the message to the queue (and this is straightforward looking at the documentation).
Where I am stuck is in understanding how to set up RabbitMQ to consume the queue by sending messages to the mailer API (calling the endpoint that was originally in my Axios request on frontend.
By looking at RabbitMQ documentation my understanding is that what they call new_task.js
:
var queue = 'task_queue';
var msg = process.argv.slice(2).join(' ') || "Hello World!";
channel.assertQueue(queue, {
durable: true
});
channel.sendToQueue(queue, Buffer.from(msg), {
persistent: true
});
console.log(" [x] Sent '%s'", msg);
need to be put in a route so that I can call it to enqueue my messages (this route replaces the route I use now directly) and needs to be adjusted based on my needs and later worker.js
is to be kept running to listen to incoming messages and that
console.log(" [x] Received %s", msg.content.toString());
setTimeout(function() {
console.log(" [x] Done");
}, secs * 1000);
is mainly where I need to put the call to the mailer route.
Am I on the right path?
Upvotes: 0
Views: 29