Email and SMS queuing in NodeJS - node.js

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?

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.

Related

Correct way to build an in-app notification service?

Background
I have a monolith Node.js + PostgreSQL app that, besides other things, needs to provide real-time in-app notifications to end users.
It is currently implemented in the following way:
there's a db table notifications which has state (pending/sent), userid (id of the notification receiver), isRead (did a user read the notification), type and body - notification data.
once specific resources get created or specific events occur, a various number of users should receive in-app notifications. When a notification is created, it gets persisted to the db and gets sent to the user using WebSockets. Notifications can also get created by a cron job.
when a user receives N number of notifications of the same type, they get collapsed into one single notification. This is done via db trigger by deleting repeated notifications and inserting a new one.
usually it works fine. But when the number of receivers exceeds several thousands, the app lags or other requests get blocked or not all notifications get sent via WebSockets.
Examples of notifications
Article published
A user is awarded with points
A user logged in multiple times but didn't perform some action
One user sends a friend request to another
One user sent a message to another
if a user receives 3+ Article published notifications, they get collapsed into the N articles published notification (N gets updated if new same notifications get received).
What I currently have doesn't seem to work very well. For example, for the Article created event the api endpoint that handles the creation, also handles notifications send-outs (which is maybe not a good approach - it creates ~5-6k notifications and sends them to users via websockets).
Question
How to correctly design such functionality?
Should I stay with a node.js + db approach or add a queuing service? Redis Pub/Sub? RabbitMQ?
We deploy to the k8s cluster, so adding another service is not a problem. More important question - is it really needed in my case?
I would love some general advice or resources to read on this topic.
I've read several articles on messaging/queuing/notifications system design but still don't quite get if this fits my case.
Should the queue store the notifications or should they be in the db? What's the correct way to notify thousands of users in real-time (websockets? SSE?)?
Also, the more I read about queues and message brokers, the more it feels like I'm overcomplicating things and getting more confused.
Consider using the Temporal open source project. It would allow modeling each user lifecycle as a separate program. The Temporal makes the code fully fault tolerant and preserves its full state (including local variables and blocking await calls) across process restarts.

What is the send message throughput of MS Teams bot?

I am developing an MS Teams app with a bot. Occasionally, the bot needs to send a proactive message to a subset of users who have installed the app.
We cannot send a message to multiple users at once. So we have to make one HTTP request per user per message. Given that batching is not possible and there are API rate limits, I want to know how scalable is this.
Specifically, I want to know the approximate time between when the first and the last user receive the messages if I send message to 5k users at once. What about 10k? 20k?
Any help is much appreciated, thanks :)
This document gives a lot of detail on the throttle limits, and the related time periods required: https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/rate-limit

Nodejs Email Sending using sendgrid web api

I am developing an email sending service, probably for sending bulk emails using sendgrid web API, but I am not able to figure out best practice for scalable system. I wish to keep a record of all those emails which failed to deliver and retry sending to those failed emails after all emails have been sent. I am using NodeJs, so just wanted to know if there is any way to speed up my process(something like sending multiple emails at the same time)
There are multiple ways to handle this, I will suggest two which seems obvious to me.
(Recommended - Easy) Use Async module's control flow option called queue Async Documentation. You can feed in all the request in form of an array of object request and then change concurrency setting to let's say 100, it'll run concurrent 100 workers at one time and to log errors make a separate mechanism and once all the values have been run through handle it separately.
Spawn multiple workers using node.js native approach.
Sendgrid offers an npm package for node.js integration, so you don't have to reinvent the wheel. It accepts messages at a high rate, so you shouldn't have problems delivering yours to sendgrid. You just dump your messages into sendgrid.
Email, being a store-and-forward system, is inherently asynchronous. That means it operates far from real time. Some messages are delivered in a few seconds, and others take hours (when they get soft--"retry later"--rejections from destination servers, for example).
Sendgrid handles this issue with a "bounces" API. (And with "bounces" features in their web back end application). Many bounces are "hard" bounces, meaning you must avoid trying to send messages to that address again. You can use the bounces API to retrieve a list of bounced messages. You should remove those addresses from your email list, and not try to send them again. (Sendgrid bans users who repeatedly send mailings with a high undeliverable rate.)
They also have an "invalid emails" API. This works like "bounces" and returns lists of addresses that are ill-formed or, if sendgrid can tell, not present on the destination server. Again, you should remove these addresses from your email list. If they're invalid now, they will be invalid tomorrow.
Sendgrid offers all sorts of tutorials on this subject.

Push notification to millions of device + Apns + node.js

My application stack is ios(front-end) and node.js(back-end). I have to send notification to devices. In my node.js part im using apns module to send notification, its working fine......
Now i have to send Mass notification like at a time consider i have 10,000 devices to notified, the logic what im following is
I'm looping through 10,000 devices and calling apns provider.
1.Why this for loop approach
I have to store each notification details in my mongodb collection, so i followed this approach.
The problem is the notification is received by some devices and that too very late(next day).
I read the link also
https://www.raywenderlich.com/156966/push-notifications-tutorial-getting-started
saying apns will reject.
Is the above approach is correct also any way to make all notification deliverer.
Please share your ideas. Thanks in advance.
If you need to process each individual notification before/after it is sent I would instead recommend a design change from a loop and have you look at job queue instead.
With this design pattern, instead of your only step being to loop over notifications and send via APN, you push these notification into a queue/messaging system and have workers which pull from the queue and process (send via APN and write to mongo) the notifications. The nice part of this design is that as your application grows you can add on more workers to handle the increased load without rewriting your application/architecture. Once you have it built it may look something like this:
I personally use RabbitMQ for my job queue, but that decision is something you need to research on your own. For example if you don't want to manage the messaging system you could look into something like AWS Simple Queue Service.
I think looping through 10,000 devices ids and calling APNS provider is not the right way forward. The documentations strictly says here node-apn readme file to reuse apn.Provider rather than recreate it every time to achieve the best possible performance.
If you send notification using arrays of device ids rather than just a device id then you will get a response from the APNS mentioning all the details for each device.

send emails at times dependent on users timezone using nodejs

I'm relatively new to nodejs and was wondering what the best approach would be in setting up a automated job between set hours (lets say 8am-10pm every hour dependent on the customers timezone), to perform a task then based on results send users emails?
The task you describe is not so simple at first:
You need to have some queue implementation, if you want something simple you can use Kue.js which uses Redis for storing the jobs. For more scaled solution would be using ZMQ or RabbitMQ.
The job processor, is the part where you have to interact with database and give you out the list of emails. Depending on the size of your email list, it's better if it could be splitted into chunks like 200 emails and then create "child-jobs"
Email sending, there are different providers and possible solutions. First your own SMTP server, Amazon SES, Mailchimp and other similar services. For the sending library there is already great, Nodemailer.
This is not the full concept, but should give you the idea. I am sorry if this is too abstract concept, but the question is not specific either.

Resources