I am developing a node.js app. In my app, I need to send blast emails and SMS to users satisfying a particular criteria. I use Gmail SMTP for emails and a third party vendor for SMS. I'm assuming firing the API's for email and sms services in a loop is dangerous. What's the right way do it?
Email and SMS queuing in NodeJS
2.7k views Asked by Jaseem Abbas At
2
There are 2 answers
2
On
You can use kue npm library for creating queue of your email or sms tasks it runs parallels. https://medium.com/@techsuneel99/how-to-send-50000-emails-at-once-in-queue-node-js-b633ef9b3b30
The time spent is obviously proportional to N, being N the size of your set. As much users you have, longer it takes. Keep in mind that requests over the network are not blocking in any case.
Anyway, unless it risks N to be thousands or millions of items, you can do it in a loop and attach a proper callback to handle responses/errors.
Otherwise, you can send an email/sms and schedule the same operation for the next element using
nextTick
(see nodejs documentation for further details). This way you'll spread all the activities over several iterations of the event loop.