Reputation: 2116
I have some information about Comet, but I want to know how it keeps a connection open (from client to server). how does it do that?
Upvotes: 2
Views: 421
Reputation: 60767
Comet, or long-polling ajax, doesn't keep a connection open in the long run.
HTTP is stateless, it sends a request and gets the response. That's it.
So, how does it look like the connection remains open? Because it's abusing an HTTP request.
When you send the request, the client waits for the response until it's coming, or until the server decides that this connection has reached its timeout.
In Comet, the server purposedly doesn't answer immediately. It only answers when he wants to send a response. This is why the client sends HTTP requests and waits for either a response or the timeout.
This way, it looks like some kind of push from server to client, when it's just abusing the timeout property of the HTTP requests.
For example, this is some comet code (with jQuery for the sake of keeping it short):
setTimeout( function comet () {
$.get( '/some/url', {}, function ( data ) {
// Process the request's response
// And recall setTimeout
setTimeout( comet, 1 )
} )
}, 1 )
Upvotes: 2