Cometd is a scalable HTTP-based event routing bus that uses a push technology pattern known as Comet. The term ‘Comet’ was coined by Alex Russell in his post ‘Comet: Low Latency Data for the Browser‘. That is the definition given in the cometd website. This is the link to that web site http://cometd.com/.
When i start working on cometd I didn’t understand push technology, scalable HTTP-based event routing bus. What i understand is the server can able to send messages to client without client request the server. In normal way of communication with server and client, the client request for something then the server respond to client but this is in reverse way when any event happened at server will send message to client hey this event has happened update with this data.
So to make this happen the comet will help us with the comet server which exists at the server side and comet client which exists at client side.
This comet is following bayeux protocol. When i start working on comet i didn’t understand clearly the bayeux protocol.
So i gone through the chat example in the comet. From here at the client side i have to first start the connection with the cometd.init, then i have to subscribe to one channel with the cometd.subscribe. later to send any thing to server i have to use cometd.publish so the server will publish this message to all clients who subscribed to the channel where i published. I didn’t understand clearly what the server is doing to make the server keep sending these messages to subscribed clients. And while subscribing i am specifying one function which will execute when ever any message came from the server. I don’t know clearly how this function will execute when server sends message to client.
Then i thought i can start working and i started using comet in my application. With only this knowledge i developed my application.
At that time i am using dojo0.4 at client side and i am using twistedcomet at the server.
Then the problems
1. it doesn’t works with IE browser.
2. the biggest problem is connection failure detection.
3. lack of synchronisation with all clients.
4. suddenly one client stops receiving messages from server but it can able to send messages.
5. how to reconnect when there is connection loss with the server
I didn’t understand what is happening at the background.
So i started knowing more about what comet client is actually doing and what comet server is actually doing and how the server can able to send messages to client without the client request for something.
Then i started understanding what is push connection. what is scalable HTTP-based event routing bus. and bayeux protocol.
Http is stateless and Http is on top of Tcp connection. The actual communication with client and server with Http protocol is, first tcp connection will establish between client and server then the client sends some request to server using Http. when the server reply to that request the connection will automatically close by either server or client.
The things happening in comet is
1. first the client will send request to server.
2. server won’t reply back it holds the connection.
3. when ever any event happens then the server will reply on to the holded connection.
so to make the connection open every time we can do in 2 ways
Streaming means while server replying to client, server sends some fixed length packets and server don’t say that the message transfer is over. So the client receives these packets of data and process it. Because the data transfer is not completed the connection was still alive.
Polling means server reply to the client and closes the connection. then client again sends connect message so the server will hold this connection until some event happened. This procedure will repeat to keep the connection open.
I understand that the connection which is hold by server is called push connection.
Then bayeux is a protocol which is used by comet. It specifies how the message pattern should be while sending message to comet server. It tells procedure to establish push connection.
Then when go through the bayeux protocol we can understand clearly how comet is working.
Really this bayeux is very good.