views:

261

answers:

2

I was just reading how FaceBook designed their chat system, and it reads: "and having that iframe's JavaScript make an HTTP GET request over a persistent connection that doesn't return until the server has data for the client. The request gets reestablished if it's interrupted or times out. This isn't by any means a new technique: it's a variation of Comet, specifically XHR long polling, and/or BOSH."

Can someone explain how you can a persistent request to a web server?

+1  A: 

Take a look at this page. I have experimented with long polling. Basically it doesn't work different from a normal XmlHTTP request (XHR, post or get) to a server. It's the server that keeps the connection open, the client (browser) just waits for a response. While the server doesn't close the connection (readyState < 4), you can do something with the response. If the connection is closed (readyState 4), the XHR restarts.

On this location you can find a very basic and imcomplete experiment (only works in Firefox), where the server sends a RGB tuple at random intervals for some time. Problem with a continuous readyState < 4 is reading out the last value sent, because you can't tell when the last response chunk is finished. Well, it may give you an idea how it could be done.

KooiInc
+1  A: 

Esentially, you just hold the request on the server until either 1) there's data available or 2) the server hits a threshold and says "forget it, reestablish so I know you're really still there". The difficulty with this approach is the scalability of the server-side, since typically web servers are designed to execute as quickly as possible, and spawning lots of threads/processes for the incoming "long-held" requests is difficult.

This long-held request is typically Xhr, if on the same domain, or JSONP if cross-domain.

We've written a full comet client for our IIS/ASP.NET Comet server (WebSync), which you can check out and maybe get an idea. Browse the source for the client.js file (tack on ?debug=true to see the uncompressed version), and you'll see some references to "connect" requests - those are the long-poll requests to the server, which wait for ~25s each request, assuming no data arrives.

jvenema
Strange. No points for a 'pretty well' answer, but all the credits to someone taking the oppurtunity to advertise his product. Well, may be jvenema can re-explain the basics himself. My answer is deleted.
KooiInc
<sigh> No need to get angry Kooilnc; now people who would have referenced your post are unable to do so. I've updated my answer to give more details, since apparently you don't like to share yours if you don't get a pat on the back for it..
jvenema
Actually it was the hidden advertisement that irritated. The pat on the back doesn't bother me at all. I suppose you mean well, so I'll undelete my answer. The therein mentioned experiment aims at a simple long poll (client side) technique without the use of an iframe. To my surprise today I discovered that the avast webscanner blocks all readystates < 4.BTW jvenema: are your origins dutch?
KooiInc
Hey Kooilnc, thanks for the undelete; I freely admit that I'll refer people to my product, but only if and when it makes sense. In this scenario, I wasn't even recommending it, but giving access to the source for part of it, so I'm glad you understand. And yes, I'm Canadian, but 100% Dutch roots.
jvenema