The Gallery is deprecated. No new Gallery components may be submitted, and modifying existing components is disabled. For more information please read the Gallery Deprecation blog post. This is a static snapshot of the Gallery for archive purposes.
See 7 more by this user. Created: 11/17/10
Last CDN Push: 11/17/10
Build Tag: gallery-2010.11.17-21-32
Project: YUI 3
License: YUI BSD
YUI Version: 3.3.0 PR1
Free for use.
Implements the W3C specification for Server-Sent Events. Since the officially published specification (http://www.w3.org/TR/eventsource/) was last updated in 2009, when question arose about implementation details, the editor's draft (http://dev.w3.org/html5/eventsource/) was used for clarification.
Server-Sent Events are a form of Comet communication. As opposed to Web Sockets, Server-Sent Events can be implemented over regular HTTP using a long-lived connection. The response must have a content type of "text/event-stream" and output buffering must be turned off to ensure the browser receives timely data.
There are two ways to implement an event stream. One is to use long polling (http://en.wikipedia.org/wiki/Comet_%28programming%29#Ajax_with_long_polling), where a request is kept open until data is sent. At that point, the connection is closed by the server and the client reopens the connection to wait for more data. This auto-reconnect feature is central to Server-Sent Events. The second approach is to use HTTP streaming (http://en.wikipedia.org/wiki/Comet_%28programming%29#Streaming), which keeps a single connection open to the client and sends repeated data through the connection without closing it.
Firefox, Safari, Chrome, and Opera all support HTTP streaming either through XMLHttpRequest or through a native implementation of EventSource (the main object for Server-Sent Events). Internet Explorer through version 9 does not support HTTP streaming. For optimal compatibility, it's recommended to use long polling as the primary mechanism for your event stream.
However, it is possible to optimize for the browsers that support HTTP streaming. When this module is run in a browser that doesn't support HTTP streaming, a special "X-YUIEventSource-PollOnly" header is sent. You can use this to determine whether to use long polling or HTTP streaming. The following is a simple example in JSP:
<%@page contentType="text/event-stream" buffer="none"%>
<%
//check for poll-only header
String header = request.getHeader("X-YUIEventSource-PollOnly");
//check every so often to see if there's new data
while(true) {
//sleep for a second - simulate waiting for data
Thread.sleep(1000);
//output the current time, ensure there are two trailing newlines
out.print("data: " + (new java.util.Date()).toString() + "x\n\n");
out.flush();
//if it's a poll-only request, break the loop, which ends the request - the client will reconnect
if (header != null){
break;
}
}
%>
This module implements most of the Server-Sent Events spec, by does not implement the retry option due to lack of clarity as to how this should be implemented.
<script src="http://yui.yahooapis.com/3.3.0 PR1/build/yui/yui-min.js"></script>
YUI({
//Last Gallery Build of this module
gallery: 'gallery-2010.11.17-21-32'
}).use('gallery-eventsource', function(Y) {
var src = new Y.EventSource("stream.php");
src.on("open", function(event){
console.log("Connection opened!");
});
src.on("message", function(event){
console.log("Data received: " + event.data);
});
src.on("error", function(event){
console.log("Error!");
});
});
Subject | Author | Date |
---|---|---|
Any Examples? | Andrew Wooldridge | 11/18/10 |
Re: Any Examples? | Nicholas C. Zakas | 01/12/11 |
All code on this site is licensed under the BSD License unless stated otherwise.