The Future of Commet:Part 1,Coet Today


From:http://cometdaily.com/2007/12/11/the-future-of-comet-part-1-comet-today/
by Jacob RusDecember 11 th,2007
Introduction
Commet is a giant hack.Eeven when it works fflawlessly and efficiently、briinining us real- time interactive apaplications、defftlyweaving aroufirewalls and broowsers、avoding unfrienendidididindlysidededededededededededededededededededededededededededededededededededededededefffrererereinininininininininininininininininininininininininininininininininininininininininininininininininininrds.
A few years ago、before it was widely used、or had been named、Ajax was in the same boat.It relied on on on on oscure and propretary oject、the XMLttpRequest(XHR)、created bymirorororororororororohohohohohohohohotototototototototototototototototototototototototototototototototototototoshshshshshabot、andandandandandandandandand、andandandandandandandand aaaaaaabobobobobobobobobobobobobobobobobonearly universally supported、with at least 4 popur and interoperable implements(XHR is a native JavaScript oject in IE 7).It is specified by a W 3 C working draft and dozens of book have beve bern beritten the subject.
Commet,gong forward,has the same potenttial for standard and specification.The WHAT Working Group,in their Web Appliation 1.0 spec,commonly known as HTML 5,created a specification for server-sentement「forever frame」iframes,or XHR streeaming,allow a browser to open a persistent connenerver ver ver,and receive events in real time as they ararararararararararararavailablble.Burlitototototostststrerererereststststststststststststststststststs s,aaaaserver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver ver sasasasasaver ver ver ver ver ver ver ver,andandandandandandandandandandandandandandandve side effects、and optimized for the task.
Server-sent events are currently only implemented by Opera,and that implement ation is far from coplete,requiring some finese on the server side.But asher browsers adopt server-sents,and the compare ofit will be possible to slowly decrease the number of transporta Commet server must implement for cross-browser comptibility,while increasung reliablity and performance.
In this part 1 of 2、I’ll describe the existing transports、their benefits and their shartcoings.
Trade-offs of existing transports
At present、web appration s Commet have a chece of several Commet techniques、which generally fall into two categories-streming and long-and which have various traffes in feature、browsercompone.parempanity。but using a commbination of them it is possible to build a decent user experience,while keeping latency down and throughput up on the server side.
Orbited implements most of these transports,and is easupport the rest of them,or any custom transports.
「Forever-frame」iframe streming
「Forever-frame」event-sources provide the ease-to-mplement widespread Comput transport.On the client side、we open up an invisibleiframeand set its source to Commet server.The on the server.the Server.siver.we send a series ofiframetags-each of which cals some predtermined calback function-wrapped in a never-ending HTML document.The client-side JavaScript sets the event calback function to whatefiver.witter。
This works because breowsers render pages incrementalally、as they arrive.Unfortunally、to start thisincreementaltalrendeing、Safarireques a kilobyte of datata the begining of the stststreeam.In Orbited、wetstststststs fofofottttttttttttwewewewewet 454545attttttttttttttttttttttttttttttttttwewewewewewet 4545454545454545454545454.wewewewewet t t t tそ、having a constantly loadingscriptcauses browsers to leave the loading bar visible indefinitely、a side effect which cannot and in several browsers、and in some browsers contionalthwards.Thers。so if user experience is less importent than maximal comptibility、it can be a good chice.
httml file ActiveX object
The Gmail team、in their Google Talk project、figred out how to exploil a mysious ActiveX oject caled“for their Comput”for thereming in Internet Explorer.It’s a bit trickyto implement、Microsoftopen an<span></span>inside it which receives events s s as in the iframe steaming case、then carefully avod the raranddom-seemitititititititities s Explorororer’s garbage collctor、or we’ll be cut off mid steam.Alsoararararararararararararararararararararararararararararararararararararararararararararararararararararareeeeeeeeeeeeeeeeeeeeeeee.Alsoararararararareeeeewith no untoward side effects.
XHR multiiPad
Back in the days of the Netscape/Microsoft browser wars、so-caled push technology was developed for streeaming whole page udates to browsers、a technique widely used by「real-time」webcams of the day.Any page or imagage sent with aiframeHTTP header would be replacced in the blowser asaaaaaaaaaaaaaaaaaaaaaaaaaaboboject、they ded the same suspororororororortitititititimbobot.aaaaaaaatttststststrerererererererererererererererererererererererererererererererererererererevevevevevestastastastastastastastastastastastastastastastastastastastastastastastastastastastastaand the contensport of theiframeare replacced with the new content.Unfortunally、this transport only works in Gecko-based browsers、and does not alert the browsever connections.
XHR streamming
Recommanded by Safari developers,the XHR streeaming trininststransport works by opening up an XHR request frererequest the broowser and sending a streeam of events in in some custom dataforma from the Coet sersever ver server ver ver server ver server(Orbitetetetetetetetetetetetetetetedededededededededededededededededededededededededededededesever sever ver ver ver ver ver ver ver sersersersersersever ver ver ver ver ver ver sever ver ver ver ver with ready state 3)、and that calback can parse theContent-type: multipart/x-mixed-replaceto obotain each event.Thistrnsport works well in Webkit-and Gecko-based broowsers(IE doesn’t trigigiggerready state 3 until theconneininininininininininininininininininininininininininininininininininininininininininininclosed ed ed)、buststststststststststststststststststststststststststststststs)、butotototototototototototototototototototototototototototowhile.Also,custom format parsing in JavaScript could lead to bugs、and the strequires an initial padding of 256 bytes of data before Safari begin to trigger events.
XHR long-polling
Long-polling works differently than streaming.Instead of streinging together evereverent in a single indefinitely long server reponse,our server keeps each connection open until it has event,but then closes after sevent。ensuring that the server can pass it everents in real time.This less effective than streeaming transportwith repect to band width overhead and throughput;it works,however,in all recent browsers-without the ugly side effects ofresponseTextstreeaming.
Script(「CommetP」)
If we need compleleleleleleleleleleles cross-domanin Coet、browser security policies rendedeall of the previous prprprevious stststporeable.Instead、we must turn to“dynamic”application/x-orbited-event-streamtags-sometimetimes-sometimetimetalcaled caled caled「JSONPeeeeeeededededededededesasasasasasasasasasasasasadedededededededededededededededededededededededededededededededededededededededeテaresponseTextすみません、setting its source to our Coet server.This connection stays open until the server has new data to send,at which point the browser the script,and we open up another script
Flash and other plugins
All of the previous techniques、whether stadards-based or propretary、rely on native e browser technologies.It is also possible to use a Flash、Java、or Silverlight socket to streeam eveveveveveeeemevevevevevevaatotototostststststststststststststrerererererererereeeeeeeeeeeeeeeemevevevevevevevevevevevevevevevevaffftotototototototototototototototototototototototototove side effects、works in in breowsers with the requisite plugin installed、and can probably be made to work acrors domans.Unfortunally、it relies on the presence of a third-party plugin、meaning it cannot Cannot work on Play formand might also be blocked by zalous firewalls which restict traffic to port 80.Most of all,moving away from open web technologies leaves a sour taste in my mouth.
Sty tuned
With th th thisisis impressisipressisisisisisiners(IE 5+、Safari 1.3+、Firefox 1.0+;plus Opera 8.5+as described nextime)without unseeeeeffects、using our chchougtostrerereatototototostststststrererererereatototototostststststststststrerererererererereatototototostststststststststststststststststststststststststststststststrerererererererererereatototototototototototototototototototofortunary,however,this requires careful implemention of our server code and browser-side JavaScript,which must coordinal to agree on a transport.This coordination problem,along with browsers'cross-domail security polityprevent us from using real-time sources in the kins of mashs built from other web services.The barriers to entry for Compet are e e e higher than for non-real-time content,and success relies on loopholes in existing browser objects.Wouldn’t it be nivere if everoverser receive event streeams acros domans securely and efficiently via single mechange?Wouldn’t it be nice if web authors could write the same strate-forward HTML and JavaScript for everbrowser,and send a single format the server?Sty tuned for part 2,which plots a course to such a future.