Willy G Willy G - 1 year ago 60
Node.js Question

How to send multiple requests(?) objects to browser using node js

I'm writing a web app which will show that 'something' has been deployed to different environments (e.g. test, QA, prod) and the status of the deployment.

Before this happens I send an order form to the browser using res.send as below.

res.send({order_form : JSON.stringify(order_form)});

This is simply to show that the order has been received and that the automatic deployment will follow.

My thought was to simply do another res.send after, update the browser with the information that the order has been deployed to test, and one thereafter to show that it has been deployed to QA... etc.

The problem I'm having is that you can't do multiple res.send updates because it sets the header and everything in the response.

So after googling I found that I can either do a res.write or a res.render, but when I add one of those either after or before the res.send, I get the same error message (can't set header).

So what I'm asking is how would I do this following the conventions of web programming. I cannot do one res.send in the end, as the status of the deploys must be updated dynamically in real-time.


Edit: a bit unsure of the correct terminology in the question, so added a (?)

Answer Source

I am pretty sure web sockets is what you're after. A popular node.js framework is socket.io

Check out this example: http://socket.io/docs/#using-with-node-http-server

Using websockets you can setup your server to do the processing, and emit an event back to the client. The emit event is like your res.send().

In the client, you can listen to this event and update the UI in the browser accordingly.