Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display HTML to the browser incrementally over a long period of time?

Do I need to pass back any HTTP headers to tell the browser that my server won't be immediately closing the connection and to display as the HTML is received? Is there anything necessary to get the HTML to incrementally display like flush()?

This technique used to be used for things like chat, but I'm thinking about using it for a COMET type application.

like image 833
cgp Avatar asked Jun 03 '09 13:06

cgp


2 Answers

Long polling is a common technique to do something like this; to briefly summarise, it works as follows:

  1. The client sends an XHR to the server.

    • If there is data ready, the server returns this immediately.
    • If not, the server keeps the connection open until data does become available, then it returns this.
    • If the request times-out, go back to 1).
  2. The page running on the client receives this data, and does what it does with it.

  3. Go back to 1)

This is how Facebook implements its chat feature.

This article also clears up some of the misconceptions of long-polling, and details some of the benefits of doing so.

like image 167
Alex Rozanski Avatar answered Oct 11 '22 19:10

Alex Rozanski


The client will close the connection when it does not receive any data for a certain time. This timeout cannot be influenced by HTTP headers. It is client-specific and usually set to 120 seconds IIRC.

So all you have to do is send small amounts of data regularly to avoid hitting the timeout.

like image 29
Michael Borgwardt Avatar answered Oct 11 '22 19:10

Michael Borgwardt