ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Web Server Server-Sent-Events (SSE) Tutorial

Updated on June 19, 2013
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.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)