0
votes

I'm using busboy with express.js 4 and node.js to receive via an HTTP POSTa set of field values and an uploaded file from a form in the browser. The file will then be processed (data extracted, new documents created in the database etc.) with the possibility of some of the updates not being successful. What I want to do is update the user what is going on during the processing and then the outcome when all is done.

To do this I want to use HTML5 server sent events. Before some suggests it, I can't use websockets.

When I declare:

var eventSource = new EventSource('/API/upload');

in my browser I see an HTTP GET on the server to this URL. What I want is for eventSource to listen for server sent events from the HTTP POST handler, the route that receives and processes the uploaded metadata and file. My expectation was that in my app.post(... handler I could put:

res.writeHead(200, {
  'Content-Type': 'text/event-stream',
  'Cache-Control': 'no-cache',
  'Connection': 'keep-alive'
});
res.write('\n');

and then push down status updates using:

res.write(message'\n\n');
res.flush(); // I'm using compression

finally terminating the exchange with res.status(200).end(); after all the processing, updating etc. is done. But of course the browser isn't listening for that EventSource, it's listening for one on the GET route

So, how can I:

  • configure the browser with an EventStream object that doesn't automatically make a GET request when I instantiate it?
  • and instead listen for events being sent back from the POST event handler on the server?
1

1 Answers

0
votes

David. You cannot do it this way. You could use a PUT request to define where the data is going. E.g. PUT /API/upload/1234. Then you could point your event source at the server to get updates.

new EventSource("/API/upload/1234");

That way the server can report on the process of the job.