简体   繁体   中英

Send emails to multiple subscribed clients in node js

I have a node js site on local system where user is notified for trending articles or post by email. My code works like:-

app.get('/:articlename', (req, res)=>{
conn.query(`SELECT views,isAlreadySent FROM article_table WHERE article_name='${req.params.articlename}'`, (err, results, fields)=>{
// if views greater then 5000 views and isAlreadySent is false then send email to subscribed user from database
    })
});

The above code is long I am showing you guys shorter version. Everything works fine. My code is that when any user views any article it triggers email sending method and if that article have views more than 5000 then it's email will be sent to all subscribed users but the subscribed user can be thousands so how can I send email to all subscribed user without lagging the current viewer

I want to make user notified like pinterest, medium notifies millions users by mailing

I mean when any user request the article page a block of code will check the views and if views greater then something then an email will be send to multiple users. Number of users can be large and if I sent email at the time when user request the article then it can be unusual delay for that user. I want that if views are greater then something then it should post any bool value to another program and that program should email to all behind the scenes without disturbing the user or it can be done by any other trick please tell

First render the view then send email.

app.get('/:articlename', (req, res)=>{
  // DO YOUR STUFF
  // Render the article
  res.render('view');

  // Now process email stuff
  conn.query(`SELECT views,isAlreadySent FROM article_table WHERE article_name='${req.params.articlename}'`, (err, results, fields)=>{
    // if views greater then 5000 views and isAlreadySent is false then send email to subscribed user from database
    // SEND EMAIL HERE.
  });
});

I got the answer. I can make another program different from that website which will run in background and it will check the articles more then 5000 views which are unmailed and email it to subscribed users. But I have another question that can we run a background process in AWS?

Well the classic and easiest one is to add a loop. Either for-loop or while-loop. So you might do something like this:

for (let i=0;i<viewers.length;i++) { //assume that viewers is a list / array
    //sendmail()
}

this is the code for sending mails at the same time. To avoid lagging, add delays. If you wanna do it on runtime, make a while loop that condition is to detect that the list to send mail is not null. at the same time, add mail address to the list when you get e-mail address. This is not the best way, of course, but it solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM