Web Server Server-Sent-Events (SSE) Tutorial

Source

(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.

Overview
HTML5 SSE is web programming model like Comet that uses Javascript to create the data streaming from the server. SSE is part of the web development model considered to be Push technology. This push mechanism can be achieved using a variety of mechanisms aside from SSE. Push technology is a simple concept. Push technology requires a persistent Http connection with the server. While the connection is open, the server can continue to send back responses to the client.

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.

function executeSSE(){
if(typeof(EventSource)!=="undefined")
  {
 	var source=new EventSource("test_sse.php");
source.onmessage=function(event)
  	{
  	document.getElementById("outResult").innerHTML+=event.data + "<br>";
  	};

  }
else
  {
  	 	
	
		xhrPolling();
document.getElementById("outResult").innerHTML+=event.data + "<br>";


  }


function xhrPolling() {
  var xhr = new XmlHttpRequest();
  xhr.open('GET', 'test_sse.php', true);
  xhr.onreadystatechange = function () {
    if (xhr.readyState == 4) && xhr.status == 200){
        if (xhr.status > 0) {
            xhrPolling();
        }
    }
  }
  xhr.send();
}

<body onload="javascript:executeSSE()">
Push Technology
Push Technology | Source

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
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.

CGI
The CGI API has a push method that allows a script to push animated pages to the client.
use CGI::Push;
$pg = new CGI::Push;
$pg->do_push(-next_page=>\&new_page);
-or-
use CGI::Push qw(:standard);
do_push(-next_page=>\&new_page);

Pushlet
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.

Flash/Flex
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.

XMPP
Extensible Messaging and Presence Protocol is yet another protocol to data streaming.

BOSH
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.


Source

APN
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.

WebSocket
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.

Conclusion

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.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working