When we want to display some data in the browser in real time, how?
There are many ways to implement, the simplest is to set an interval, Ajax constantly send requests, this way the most obvious flaw whether or not new data will always send the request, and this interval if set long, timeliness is too poor, If the interval is set short, each client is constantly sending a large number of requests that affect server performance
Another is the use of server push technology, based on the way of long polling:
At the outset, this is also in the constant request server side, and ordinary Ajax constantly request what is the difference? This is a process that has not yet been known for long polling. In-depth study, found the difference is very big
The approximate process for long polling is:ClientSend the request via AJAX, fetch the data on the server side, if there is no data then the thread waits, there is data to wake up the thread and return the data, close the connection,ClientAjax callbacks, whenXMLHttpRequest's readystateAt 4, continue to request the server and establish the connection again.
So it seems that there is no timeliness in the way of long polling, and server-side access to data, there may be more than one data, The client can retrieve all the information on the current server at once, thus improving the efficiency.
How the server pushes Comet long polling is different from the way ordinary Ajax constantly requests