Comet Server - The Real Time Ajax
Real time web is emerging as one of the most important elements of web development.In simple words, it is all about information available to you as soon as it is produced. It allows web pages to be more dynamic. Advancements in web search technology coupled with growing use of social media enable online activities to be queried as they occur. Even world's topmost social networking sites FACEBOOK and TWITTER are using this concept for newsfeed, status updates and news alerts.
Real time web is emerging as one of the most important elements of web development.In simple words, it is all about information available to you as soon as it is produced. It allows web pages to be more dynamic. Advancements in web search technology coupled with growing use of social media enable online activities to be queried as they occur. Even world's topmost social networking sites FACEBOOK and TWITTER are using this concept for newsfeed, status updates and news alerts.
Now lets talk about Comet.
What is Comet?
Wikipedia defines comet as "a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it. Comet is an umbrella term for multiple techniques for achieving this interaction". It is an essential technique for any real-time event-driven applications, where the majority of events occur on the server and data must be "pushed" frequently to the client.
For example in case of financial trading applications, pricing updates occur frequently and must be relayed to the client at a high rate.
Need for comet server
A web server can send anything to the web browser iff the web browser specifically requests it. HTTP servers cannot send data to their clients whenever they want, there is no way to propagate an update to a browser. This limits prospect of web-based collaborative applications. Then came the not-so-great solution and that was long lived HTTP connection.
The concept of long lived HTTP connection is nothing complicated. Once the browser establishes a connection with the web server, instead of closing the connection, the server keeps it open as long as it wants. If the connection is kept open for one hour, the web server can send data to the client whenever it wants in that time period. This opened the almost closing door for webapps that need N-way collaboration.
But it has a drawback, different web browsers behave differently to long lived HTTP connections.
The second problem was, popular web servers like Apache are not the best piece of software for maintaining lots of HTTP connections. So, wat all we need is a separate web server. This class of web servers are known as Comet servers and they have no problem of scaling up to thousands of long lived HTTP connections.
How does it work?
Comet applications use long-lived HTTP or HTTPS connections between the client and server, through which the server can push new data to the client as it becomes available. Unlike traditional Web servers, which deliver a single payload and then immediately close the connection to the client, Comet servers must maintain a continuous connection to each client for the duration of the session. A true Comet implementation requires a very different kind of server architecture to be efficient and scalable.
How does Comet differ from traditional data transfer and Ajax?
In the original request-response model of the Web, a browser receives a complete Web page in response to each request.
Ajax overcomes this by doing away with the need to receive a new page and instead simply request a data response from the server. It refreshes a piece of data instead of complete web page. However, Ajax is still a request-response model, and since Ajax requires that the client initiates each interaction, data cannot be sent at will from the server.
Comet is a push approach, decreasing latency and server load. Once the client has made the initial connection, the server sends updates as they occur, without the client needing to issue further requests. .
Comet can easily provide more simultaneous connections than a traditional web server, and faster data transit between the client and the server.
With Ajax, the action is taken by the user and with Comet, it's an action from the server. Currently Comet is a popular technique for browser-based chat applications since it allows the server to receive a message from one user and display that message to another user. Some web applications that currently use Comet are Google's GTalk, Meebo's chat, and Facebook chat.
Ajax and Comet differ in the expected length of the request. Ajax uses a quick request-response to update or get new information from a server, while Comet typically opens a longer connection to get information as it is available from the server.
To summarize:
The disadvantages in AJAX approach are:
- Unnecessary calls are made to the server, even though there may be no update.
- With number of clients increasing, server may get overloaded. This stalls the server with few hundred clients.
Whereas comet technique pushes the data to the clients when the server side data changes,
avoiding unnecessary round-trips to clients. This results in increase in performance of network.
The heart of the internet is communication and with growing number of users on social networking sites, comet-servers cannot be ignored. The next generation of web applications will not only need to be responsive to one user, but also facilitate speedy communication between many users. The quicker we can chat, message, poke, tweet as well as see and respond to these actions, the better!