The browser instantiates, using Ajax, a single polling to the server to get a single push back. So every time the application needs new data from the server it sends a new request. In this case it is necessary that the client knows that there is data to fetch. This implies that the data structure of applications does not alter during this process. However, if you provide an application that lives on permanently changing data you have to send, depending on the use case, every few seconds a new poll to the sever to get new data, even if there is nothing to transfer. That is the case if you provide chat applications or live ticker e.g. for brokers. If a couple of users have opened the application there are already some hundred or thousand requests per second. The outcome of this is a big load for your server and a highly increased traffic – your server will in a senseless way be overloaded. In conclusion, our problem is the enormous amount of polling without knowing whether the server really wants to send a new push. Let’s turn the initial situation around. And we get the solution to our problem: Comet.
The browser instantiates, using Comet, only one polling to the server to get all data in form of a long-lasting push back. That makes a new way of communication possible along with a reduction of traffic and server load. But how can the server hold the connection to the browser? Some possible solutions: One is to create a PHP socket server which handles every connection. Let’s have a look at it!
The socket functions are part of an extension to PHP. More information on the installation and the functions you’ll find at http://php.net/sockets. Chris Chabot, software engineer from the Netherlands, has developed a great class for accomplishing a socket server. His socket daemon that is also the base for his web based IRC chat can be found at http://chabotc.com/phpsocketdaemon. The performance is really amazing. Thousands of requests per second can be served by the server with a latency of under 0.20 ms.
Web developers are still using simple Ajax polling or alternatives like Adobe Flash applications to implement real-time applications. But PHP opens up lots of possibilities you should not shrink away from. Comet is the future for active communications between browser and server. I am looking forward to seeing more realizations and new ideas to solve this case.