Web applications were originally developed around a client/server model, where the Web client is always the initiator of transactions, requesting data from the server. Thus, there was no mechanism for the server to independently send data to the client without the client making a initial request. To overcome this deficiency, Web app developers can implement a technique called HTTP long polling, where the client polls the server requesting new information. The server holds the request open until new data is available. Once available, the server responds and sends the new information. When the client receives the new information, it immediately sends another request, and the operation is repeated. This effectively emulates a server push feature, which is an important part of Reverse AJAX.
A very simple demonstration of long-polling with AJAX (jQuery) and PHP. Long-polling makes near “real-time” applications possible. The client does not request new data every X seconds/minutes, the client gets new data delivered when there is new data (push-notification style). The major difference between long and short polling is that short polling sends a request to the server, get an instant answer. Do this every x seconds, minutes etc. to keep your application up-to-date. But, This costs a lot of requests. where as in long polling, a request to the server, keep the connection open, get an answer when there’s “data” for you. This will cost you only one request (per user), but the request keeps a permanent connection between client and server up.
You can download coding zip from from here #link.
In this coding sample, if you want to test this process all you need to do is, unzip this folder and place it under your web server. Here .htaccess file is used to support cross domain request. Client side has js which sends an AJAX request to server and checks on real time mode whether data.txt file is having any content changes or not. If it is yes then it will display into client side immediately in a fraction of section(gives you a feel of real time). You can implement this kind of process on your real time chat like functionality.
In order to understand it in better way, you can check this complete process under your local machine web server using firebug addon in mozilla browser. You can see the image below:-
I hope this helps! 🙂