Web Server Server-Sent-Events (SSE) Tutorial
(c) 2013 Kevin Languedoc (klanguedoc)
HTML5 SSE is an API that allows web servers to send updates to the client. This is known as push in AJAX. Usually it is the client (browser) that polls or sends request to the server and gets a response back. In contrast, push technology sends data from the server to the client based on some event. It is the server that is initiating the communication. HTML5 SSE is part of an architecture known as Push Notifications or Push Technology.
So if your application and / or architecture requires some kind interface with a server (web or otherwise) and requires some data being pushed to the client, then you have several options to choose from including HTML5 SSE. Since HTML5 SSE is not implemented in IE, not even IE10 so for any SSE implementation it is important to Comet or some other technique to handle server side push data streaming based on some event.
The primary focus of this article is on HTML5 SSE but since push is not only reserved for web application development, I will provide a broader look at what technology or solutions that are available. Also since IE doesn’t support SSE and it is still the leading browser in certain markets, we need to design interfaces that choke under pressure.
The premise of Push is to open a persistent HTTP connection between a server and a client (browser). Also most architecture assumes a web based application, this functionality could be extended to mobile native apps like APN (Apple Push Notification) and desktop (client/server) architectures.
HTML5 SSE Sample Implementation
The following is sample push implementation of Push Notification. The focus is on SSE, but the example takes into consideration the fact that the browser may not support SSE like Internet Explorer and other older browsers in which case we would implement Comet.
In this example, the script checks to make sure EventSource is defined or supported and if so to initiate a new EventSource for the server based php script. Any server based program will do such Vb.net, C#, Java, or Python. The Comet implementation is with XmlHttpRequest.
Push Implementations Options
There are several techniques and technologies that have been developed to allow a web server to send data based on an event to the client.
Comet uses the XmlHttpRequest in reverse. There are several types of implementation or designs. For instance you could create a long polling technique which simply means that the client will poll the server continuously. Then there is streaming which uses XHR to maintain an open connection with the server.
The CGI API has a push method that allows a script to push animated pages to the client.
$pg = new CGI::Push;
use CGI::Push qw(:standard);
Pushlets are server based services that publish data to the client. Instant messaging uses pushlets to bounce messages between clients. Pushlets uses long polling and XHR like Comet to send data to the client.
The Flash player is also a mechanism for streaming data (text or media) to the client. In the type of Push notification you are using the RTMP (Real Time Messaging Protocol) or Remoting to stream data. Adobe has donated the Blazeds project (JEE) to Apache. There is an article here on how to implement.
Extensible Messaging and Presence Protocol is yet another protocol to data streaming.
BOSH stands for Bidirectional Streams Over Synchronous HTTP. This protocol enable two way data streaming, usually the client and server. BOSH is not a true push notification as it provides two messaging but is highly efficient at it.
The Apple Push Notification service, which is used by the Game Center and iCloud, is a high performance TCP socket API for streaming data. To use APN, you would need to setup notifications in iTunesConnect which requires a developer license. Once the notifications is configured for your app, you can develop the notifications in your app.
Push Access Protocol (PaP)
PaP is defined by the Wireless Application Protocol. PaP is mostly used in wireless devices using Push Proxy Gateways.
WebSockets are dual communications using TCP. WebSockets are an official protocol and WebSocket is also an API developed by W3C. HTML5 includes a WebSocket implementation as does most programming languages involved with the Internet. WebSockets work by sending a stream of documents rather than bytes to the client.
Push notifications in essence have been around to a long time although it seems that it is a recent concept with a popularization of Web 2.0 and Ajax. Since TCP is the backbone of most push architectures and languages like Perl and CGI have APIs to handle push, it goes to prove that this technology has been working since the early days of the Internet.