0

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

  • Does this answer your question? [Sending emails in Node.js?](https://stackoverflow.com/questions/4113701/sending-emails-in-node-js) – George Nov 04 '19 at 13:33
  • No my question is different. It's not duplicate man –  Nov 04 '19 at 13:37
  • You mean, your view doesn't get rendered until the email is sent to all subscribers? – Shihab Nov 04 '19 at 13:39
  • 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 –  Nov 04 '19 at 13:44
  • @Mr.Explorerexplorer Sorry I misunderstood, I thought you were asking about sending the email. Yes you are right, you will need something else to do it for you, [this post](https://stackoverflow.com/questions/31009340/background-processes-in-node-js) should give you some options – George Nov 04 '19 at 13:48
  • It happens sometime friend. –  Nov 04 '19 at 13:49

2 Answers2

0

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.
  });
});
Shihab
  • 2,641
  • 3
  • 21
  • 29
0

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?