Comet is 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.[1][2] Comet is an umbrella term, encompassing multiple techniques for achieving this interaction. All these methods rely on features included by default in browsers, such as JavaScript, rather than on non-default plugins. The Comet approach differs from the original model of the web, in which a browser requests a complete web page at a time.[3]
The use of Comet techniques in web development predates the use of the word Comet as a neologism for the collective techniques. Comet is known by several other names, including Ajax Push,[4][5] Reverse Ajax,[6] Two-way-web,[7]HTTP Streaming,[7] and HTTP server push[8] among others.[9]
Contents[hide] |
[edit]Implementations
Comet applications attempt to eliminate the limitations of the page-by-page web model and traditional polling by offering real-time interaction, using a persistent or long-lasting HTTP connection between the server and the client. Since browsers and proxies are not designed with server events in mind, several techniques to achieve this have been developed, each with different benefits and drawbacks. The biggest hurdle is the HTTP 1.1 specification, which states that a browser should not have more than two simultaneous connections with a web server.[10] Therefore, holding one connection open for real-time events has a negative impact on browser usability: the browser may be blocked from sending a new request while waiting for the results of a previous request, e.g., a series of images. This can be worked around by creating a distinct hostname for real-time information, which is an alias for the same physical server.
Specific methods of implementing Comet fall into two major categories: streaming and long polling.
[edit]Streaming
An application using streaming Comet opens a single persistent connection from the client browser to the server for all Comet events. These events are incrementally handled and interpreted on the client side every time the server sends a new event, with neither side closing the connection.[3]
Specific techniques for accomplishing streaming Comet include the following:
[edit]Hidden iframe
A basic technique for dynamic web application is to use a hidden iframe HTML element (an inline frame, which allows a website to embed one HTML document inside another). This invisible iframe is sent as a chunked block, which implicitly declares it as infinitely long (sometimes called "forever frame"). As events occur, the iframe is gradually filled with script
tags, containing JavaScript to be executed in the browser. Because browsers render HTML pages incrementally, each script
tag is executed as it is received.[11]
One benefit of the iframe method is that it works in every common browser. Two downsides of this technique are the lack of a reliable error handling method, and the impossibility of tracking the state of the request calling process.[11]
[edit]XMLHttpRequest
The XMLHttpRequest (XHR) object, the main tool used by Ajax applications for browser–server communication, can also be pressed into service for server–browser Comet messaging, in a few different ways.
In 1995, Netscape Navigator added a feature called “server push”, which allowed servers to send new versions of an image or HTML page to that browser, as part of a multipart HTTP response (see History section, below), using the content type multipart/x-mixed-replace
. Since 2004, Gecko-based browsers such as Firefox accept multipart responses to XHR, which can therefore be used as a streaming Comet transport.[12] On the server side, each message is encoded as a separate portion of the multipart response, and on the client, the callback function provided to the XHR onreadystatechange
function will be called as each message arrives. This functionality is only included in Gecko-based browsers, though there is discussion of adding it to WebKit.[13] Internet Explorer 10 now also support this functionality.[14]
Instead of creating a multipart response, and depending on the browser to transparently parse each event, it is also possible to generate a custom data format for an XHR response, and parse out each event using browser-side JavaScript, relying only on the browser firing the onreadystatechange
callback each time it receives new data.
[edit]Ajax with long polling
None of the above streaming transports work across all modern browsers without negative side-effects. This forces Comet developers to implement several complex streaming transports, switching between them depending on the browser. Consequently many Comet applications use long polling, which is easier to implement on the browser side, and works, at minimum, in every browser that supports XHR. As the name suggests, long polling requires the client to poll the server for an event (or set of events). The browser makes an Ajax-style request to the server, which is kept open until the server has new data to send to the browser, which is sent to the browser in a complete response. The browser initiates a new long polling request in order to obtain subsequent events. Specific technologies for accomplishing long-polling include the following:
[edit]XMLHttpRequest long polling
For the most part, XMLHttpRequest long polling works like any standard use of XHR. The browser makes an asynchronous request of the server, which may wait for data to be available before responding. The response can contain encoded data (typically XML or JSON) or Javascript to be executed by the client. At the end of the processing of the response, the browser creates and sends another XHR, to await the next event. Thus the browser always keeps a request outstanding with the server, to be answered as each event occurs.
[edit]Script tag long polling
While any Comet transport can be made to work across subdomains, none of the above transports can be used across different second-level domains (SLDs), due to browser security policies designed to prevent cross-site scriptingattacks.[15] That is, if the main web page is served from one SLD, and the Comet server is located at another SLD, Comet events cannot be used to modify the HTML and DOM of the main page, using those transports. This problem can be sidestepped by creating a proxy server in front of one or both sources, making them appear to originate from the same domain. However, this is often undesirable for complexity or performance reasons.
Unlike iframes or XMLHttpRequest objects, script
tags can be pointed at any URI, and JavaScript code in the response will be executed in the current HTML document. This creates a potential security risk for both servers involved, though the risk to the data provider (in our case, the Comet server) can be avoided using JSONP.
A long-polling Comet transport can be created by dynamically creating script
elements, and setting their source to the location of the Comet server, which then sends back JavaScript (or JSONP) with some event as its payload. Each time the script request is completed, the browser opens a new one, just as in the XHR long polling case. This method has the advantage of being cross-browser while still allowing cross-domain implementations.[15]
[edit]History
[edit]Early Java applets
The ability to embed Java applets into browsers (starting with Netscape 2.0 in March 1996[16]) made real-time communications possible, using a raw TCP socket[17] to communicate between the browser and the server. This socket can remain open as long as the browser is at the document hosting the applet. Event notifications can be sent in any format — text or binary — and decoded by the applet.
[edit]First Comet applications
In March 2006, software engineer Alex Russell coined the term Comet in a post on his personal blog.[18] The new term was a play on Ajax (Ajax and Comet both being household cleaners common in the USA).[19][20][21]
Even if not yet known by that name, the very first Comet implementations date back to 2000,[22] with the Pushlets, Lightstreamer, and KnowNow projects. In 2006, some applications exposed those techniques to a wider audience:Meebo’s multi-protocol web-based chat application enables users to connect to AOL, Yahoo, and Microsoft chat platforms through the browser; Google added web-based chat to Gmail; JotSpot, a startup since acquired by Google, built Comet-based real-time collaborative document editing.[23] New Comet companies and enterprise solutions were created, such as the Java-based ICEfaces JSF framework (although they prefer the term "Ajax Push"[5]). Others that had previously used Java-applet based transports switched instead to pure-JavaScript implementations.[24]
[edit]Alternatives
Browser-native technologies are inherent in the term Comet. Attempts to improve non-polling HTTP communication have come from multiple sides:
- The HTML 5 draft specification produced by the Web Hypertext Application Technology Working Group (WHATWG) specifies so called server-sent events,[25] which defines a new Javascript interface
EventSource
and a new MIME typetext/event-stream
. Experimental implementation of this feature was introduced in Opera 9.[26] - The HTML 5 WebSocket API working draft specifies a method for creating a persistent connection with a server and receiving messages via an
onmessage
callback.[27] - The Bayeux protocol by the Dojo Foundation. It leaves browser-specific transports in place, and defines a higher-level protocol for communication between browser and server, with the aim of allowing re-use of client-side JavaScript code with multiple Comet servers, and allowing the same Comet server to communicate with multiple client-side JavaScript implementations. Bayeux is based on a publish/subscribe model, so servers supporting Bayeux have publish/subscribe built-in.[28]
- The BOSH protocol by the XMPP standards foundation. It emulates a bidirectional stream between browser and server by using two synchronous HTTP connections.
- The JSONRequest object, proposed by Douglas Crockford, would be an alternative to the XHR object.[29]
- Use of plugins, such as Java applets or the proprietary Adobe Flash (using RTMP protocol for data streaming to Flash applications). These have the advantage of working identically across all browsers with the appropriate plugin installed and need not rely on HTTP connections, but the disadvantage of requiring the plugin to be installed
- Google announced[30] a new Channel API for Google App Engine,[31] implementing a Comet-like API with the help of a client Javascript library on the browser. It could be later replaced by HTML5 WebSocket, however.
[edit]See also
[edit]References
- ^ Krill, Paul (September 24, 2007). "AJAX alliance recognizes mashups". InfoWorld. Retrieved 2010-10-20.
- ^ Crane, Dave; McCarthy, Phil (October 13, 2008). Comet and Reverse Ajax: The Next-Generation Ajax 2.0. Apress.ISBN 978-1-59059-998-3.
- ^ a b Gravelle, Rob. "Comet Programming: Using Ajax to Simulate Server Push". Webreference.com. Retrieved 2010-10-20.
- ^ Egloff, Andreas (2007-05-05). Ajax Push (a.k.a. Comet) with Java Business Integration (JBI) (Speech). JavaOne 2007,San Francisco, California: Sun Microsystems, Inc.. Retrieved 2008-06-10.
- ^ a b "Ajax Push". ICEfaces.org. Retrieved 2008-07-21.
- ^ Crane, Dave; McCarthy, Phil (July 2008). Comet and Reverse Ajax: The Next Generation Ajax 2.0. Apress. ISBN 1-59059-998-5.
- ^ a b Mahemoff, Michael (June 2006). "Web Remoting". Ajax Design Patterns. O'Reilly Media. pp. 19; 85. ISBN 0-596-10180-5.
- ^ Double, Chris (2005-11-05). "More on Ajax and server push". Different ways of doing server push. Retrieved 2008-05-05.
- ^ Nesbitt, Bryce (2005-11-01). "The Slow Load Technique/Reverse AJAX". Simulating Server Push in a Standard Web Browser. Retrieved 2008-05-05.
- ^ HTTP 1.1 specification, section 8.1.4. W3C. Retrieved 30 November 2007
- ^ a b Holdener III, Anthony T. (January 2008). "Page Layout with Frames that Aren't". Ajax: The Definitive Guide. O'Reilly Media. p. 320. ISBN 0-596-52838-8.
- ^ Johnny Stenback, et al. (March–April 2004). “Bug 237319 – Add support for server push using
multipart/x-mixed-replace
with XMLHttpRequest”. Mozilla bug tracking. Retrieved 29 November 2007. Also see:
Alex Russell (6 August 2005). “Toward server-sent data w/o iframes”. Alex Russell’s blog. Retrieved 29 November 2007. - ^ Rob Butler, et al. (June 2006). “Bug 14392: Add support for
multipart/x-mixed-replace
to XMLHttpRequest”. Webkit bug tracking. Retrieved 29 November 2007. - ^ http://msdn.microsoft.com/en-us/library/ie/hh673569%28v=vs.85%29.aspx
- ^ a b Flanagan, David (2006-08-17). "13.8.4 Cross-Site Scripting". JavaScript the Definitive Guide. The Definitive Guide.O'Reilly Media. p. 994. ISBN 0-596-10199-6.
- ^http://web.archive.org/web/19961115203505/www27.netscape.com/comprod/products/navigator/version_2.0/index.htmlNetscape.com from 1996 (via Archive.org)
- ^ http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html
- ^ Alex Russell (3 March 2006). “Comet: Low Latency Data for the Browser”. Alex Russell’s blog. Retrieved 29 November 2007.
- ^ K. Taft, Darryl (2006-05-12). "Microsoft Scrubs Comet from AJAX Tool Set". eWEEK.com. Retrieved 2008-07-21.
- ^ Orbited: Enabling Comet for the Masses: OSCON 2008 - O'Reilly Conferences, July 21 - 25, 2008, Portland, Oregon
- ^ Enterprise Comet & Web 2.0 Live Presentation
- ^ "CometDaily: Comet and Push Technology".
- ^ Dion Almaer (29 September 2005). “Jotspot Live: Live, group note-taking” (interview with Abe Fettig). Ajaxian. Retrieved 15 December 2007.
Matt Marshall (15 December 2006). “Renkoo launches event service — in time to schedule holiday cocktails”. Venture Beat. Retrieved 15 December 2007. - ^ Clint Boulton (27 December 2005). “Startups Board the AJAX Bandwagon”. DevX News. Retrieved 18 February 2008.
- ^ Ian Hickson, ed. (2007-10-27). "6.2 Server-sent DOM events". HTML 5 - Call For Comments. WHATWG. Retrieved 2008-10-07.
- ^ Bersvendsen, Arve (2006-09-01). "Event Streaming to Web Browsers". Opera Software. Retrieved 2009-06-01.
- ^ Hickson, Ian (2009-04-23). "The WebSocket API". w3c. Retrieved 2009-07-21.
- ^ Alex Russell, et al. (2007). "Bayeux Protocol - Bayeux 1.0draft1.". Dojo Foundation. Retrieved 2007-12-14.
- ^ Crockford, Douglas (2006-04-17). "JSONRequest Duplex". An alternative to XMLHttpRequest for long lasting server initiated push of data. Retrieved 2008-05-05.
- ^ http://googleappengine.blogspot.com/2010/12/happy-holidays-from-app-engine-team-140.html
- ^ http://arstechnica.com/web/news/2010/12/app-engine-gets-streaming-api-and-longer-background-tasks.ars
Comet (혜성)는 Web 어플리케이션 을 구축 할 때 사용되는 기술로,이 기술을 사용하면 서버 에서 발생한 이벤트 를 클라이언트 의 요청없이 클라이언트 에 보낼 수있다.
Comet은 이러한 통신을 실현하기위한 여러 기법을 정리 한 개념이다. 이러한 기술은 브라우저에 플러그인을 추가하지 않고 ( JavaScript 와 같은) 기본 기능으로 실현되는 것이다. 이론적으로는 Comet은 브라우저가 데이터를 요구하는 형태의 기존 웹 모델과는 다르다. 실제로 Comet 애플리케이션은 Ajax 와 Long polling 을 사용하여 서버에 새 데이터를 검색한다.
목차[ 숨기기 ] |
왜 필요한가 [ 편집 ]
기존의 방법으로는 웹 페이지는 클라이언트에서 요청이있을 때 만 클라이언트에 전달되고 있었다. 클라이언트가 요청할 때마다 브라우저는 서버에 HTTP 연결을 생성하고 웹 서버는 클라이언트에 데이터를 반환하고 연결이 닫힙니다. 이 방법의 단점은 사용자가 명시 적으로 페이지를 새로 실시하거나 사용자가 새로운 페이지로 이동하는 경우에만 표시되는 페이지가 갱신되지 않는 것이다. 페이지를 전송하는 데 긴 시간을 요하기 때문에, 페이지를 새로 엄청난 지연을 만들어 낸다.
이 문제를 해결하기 위해, 브라우저에 변경된 부분 만 요청 업데이트시키는 기술이다 Ajax 를 사용할 수있다. 이 방법은 데이터 트래픽이 기존의 방법보다 줄어들 기 때문에 지연의 정도도 줄어 전체 사이트의 성능 향상이라고 할 수있다. 다시 말하면, 비동기 통신을 이용함으로써 사용자는 단계적으로 데이터를 수신하면서 작업을 할 수 있기 때문에, 그런 의미에서도 성능이 향상된다.
그러나 Ajax를 사용하더라도 클라이언트가 데이터를 검색하기 전에 이에 대한 요청을 내야한다는 어려운 문제는 여전히 존재한다. 이 문제는, 예를 들면 "있는 사용자가 데이터를 보내왔다"라고하는 서버에서 이벤트가 발생할 때까지 대기해야하는 응용 프로그램을 설계 할 때 큰 장애가된다.
서버에서 이벤트가 발생했는지 여부를 정기적으로 확인한다 (이른바 폴 루프시키는)하도록 응용 프로그램을 설계하는 것은 하나의 해결책이다. 그러나이 방법은 응용 프로그램이 결국 서버 이벤트가 완료 여부의 질문에 많은 시간을 써 버리기 때문 그다지 우아가 아닌. 또한 네트워크의 대역폭을 많이 소비되어 버린다.
실현 방법 [ 편집 ]
일반적으로 Web 서버는 클라이언트의 요청을 받으면 즉시 응답을 돌려 준다.
Comet을 이용한 Web 응용 프로그램 서버는 클라이언트의 요청에 즉시 응답하지 않고 보류 상태로두고 서버에서 특정 이벤트 (대화에서 누군가가 발언 한 등)이 발생했을 때 응답을 반환. 이렇게하면 서버에서 발생한 이벤트를 즉시 클라이언트에 보낼 수있다.
관련 항목 [ 편집 ]
외부 링크 [ 편집 ]
출처 - http://en.wikipedia.org/wiki/Comet_(programming)
'Development > JavaScript' 카테고리의 다른 글
javascript - forms 객체 (0) | 2013.05.13 |
---|---|
socket.io readme (0) | 2012.12.28 |
자바스크립트 이벤트 핸들링 (0) | 2012.12.27 |
javascript random generate (0) | 2012.12.25 |
socket.io configure (0) | 2012.12.18 |