I have an Express.js server with a route that is responsible for parsing and importing a CSV file.
I'd like to report the status of the import process in realtime back to the user's browser.
Streaming responses from Express.js is straight forward:
router.get( '/test', function( _req, _res, _next ) {
_res.write( 'File import initiated.' );
// ... processing CSV in loop
_res.write( 'Processing row x.' );
// artificial delay to simulate some i/o
setTimeout( function() {
_res.write( 'File import completed successfully.' );
_res.end();
}, 2000 )
} );
Sending a HTTP request to the endpoint via CURL works as expected. In the above example two streamed responses are received immediately, followed by the final response after two seconds.
This is not the case in the browser (Chrome) however. Testing both with jQuery $.ajax
, and a raw XHR call, the entire
response appears to be buffered and returned all at once when the response is complete.
How is this type of communication normally handled? I assume socket.io is an option, but surely there's a simpler way to utalise the build int XHR or XHR2 features of the browser?