1

I am making an Nodejs and express based app where orders for certain products can be taken. In that app, as soon as an order comes it is saved to database and then a pdf is generated as order receipt. Generating a pdf take some time hence I send data to user as soon as the order is saved in database. But there is a table on the place order page where all previous orders are shown. Since pdf is generated after sending the response to user I have to continuously poll the server to check if any new orders were placed which is not optimized at all. So is there a way to send response once again to same request.here is code representation of what I want to achieve

//import Order model

app.post('/',async (req,res)=>{
      
let order = new Order(//data from req obj)
await order.save();
res.json({msg : 'order saved'});
//generate pdf and save it on cloud
res.json({pdfLink : pdf.link}) 

})
Nisha Dave
  • 669
  • 1
  • 9
  • 25

4 Answers4

1

What you're describing as your current solution is called long polling. You might consider using websockets to push the PDF link to the front end when it becomes available.

  • 1
    what I have described as my current solution is called short polling, long polling is sending a request to server and keeping it. You can read about it here https://stackoverflow.com/questions/4642598/short-polling-vs-long-polling-for-real-time-web-applications – Nisha Dave Mar 10 '21 at 10:41
1

After some research I found out that there are two ways to solve this problem

  1. as @Dustin Mackenzie stated using web sockets that is a perfectly valid solution but for only one part of application I need to do a lot of stuff like installing socket.io/implement web sockets on my own. then managing client side as well as for web sockets.

  2. Use server sent events which is very easy and it does not require me to use any specific library. It has a very wide browser support and implementing it is as easy as setting a content-type header when sending response and when using res.write(), following a specific syntax

Nisha Dave
  • 669
  • 1
  • 9
  • 25
0

As per the Documentation you are not allowed to send multiple response to single request.

Instead of that use res.write(), Will resolve your issue. It was nodejs internal function.

For more about res.write function please follow the official documentation.

  • 1
    res.write works but the problem is the response will be reflected on client side when I use res.end() and so the user will have to wait equally long. – Nisha Dave Mar 08 '21 at 08:19
-1

You can't respond twice to one request. You need to combine your responses into a single response. For example:

res.json({
  msg : 'order saved',
  pdfLink : pdf.link
})
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    as I said earlier, creating pdf takes some time so I dont want the user to wait that long. That is why I have asked this question – Nisha Dave Mar 08 '21 at 04:45
  • 1
    in that case, if the pdf generation support streaming, you can pipe the output stream directly to the response. user will receive it as soon as pdf generation starts. – Tuan Anh Tran Mar 08 '21 at 04:51