[Fixed] How to send data more than once for a single request

Issue

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}) 

})

Solution

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

Leave a Reply

(*) Required, Your email will not be published