0

I have a web application running behind nginx. Some pages are accessible via http, some others via https. I have some "pages", which are rather streams as the application does not close the connection and feeds data as they come. The feed then looks like this:

 TIME1 MESSAGE1
 TIME2 MESSAGE2
 ...
 TIMEn MESSAGEn

After each line I write "\n" and then call flush(). Over http, it works correctly and my client can listen to new data. However, over https the client is not receiving any data until the connection is closed.

 ServletOutputStream stream = applicationModel.getOutputStream();
 OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
 BufferedWriter writer = new BufferedWriter(streamWriter);
 while (true) {
     wait();
     writer.write(newMessage);
     writer.flush();
 }
Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • flush() flushes only the output buffer of your application, but nginx itself can buffer too (other servers do it too). See http://stackoverflow.com/questions/20018803/flush-output-buffer-in-apache-nginx-setup – Steffen Ullrich Sep 10 '14 at 08:04
  • If you post it as an answer I will accept it. It it strange however that only https requests are buffered. – Vojtěch Sep 10 '14 at 09:35

1 Answers1

1

Unless the application is tightly integrated with the web server a flush on the writer will only flush the buffers inside your application, so that the data gets send to the web server. Inside the web server there are more buffers, which are necessary to optimize the traffic by sending larger TCP packets and thus decrease the overhead for the data. And, if you use SSL there is yet another layer to watch, because your data will be encapsulated into an SSL frame which again adds overhead, so it is good to not only have a few bytes payload inside. Finally you have the buffering at the OS kernel, which might defer the sending of a small TCP packet for some time if there is hope that there will be more data.

Please be aware, that your wish to control the buffers is against a fundamental design concept of HTTP. HTTP is based on the idea that you have a request from the client to the server and then a response from the server, ideally with a known content-length up-front. There is no idea in the original design of a response which evolves slowly and where the browser will update the display once new data arrive. The real way to get updates would be instead to let the client send another request and then send the new response back. Another way would be to use WebSockets.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172