HTML5 Server Send Events using Node.js or Jetty

October 21st, 2012 by

The HTML5 working draft describes different techniques to push information from a server to the client and the one described in this tutorial are Server-Send Events (SSE).

Using Server-Send-Events eliminates the need to poll a server periodically for information using AJAX and is really easy to implement because of the simple specification and the fact that nearly all modern browsers already implement this specification.


The Client Side

Registering for Server Send Events (SSE) is quite easy .. simply create a new EventSource object that is bound to the URL where the events are propagated.

In the next step just add an event listener to the source object – the function is called each time that a  SSE is received.

Finally an event is bound to a button to stop listening to SSEs using the EventSource’s close method.

For the full specification I’d recommend to take a look at the W3C Working Draft or Eric Bidelman’s excellent tutorial.

<!DOCTYPE html>
<meta charset="utf-8" />
 <input type="button" id="stopButton" value="Stop Listening"/>
 <div id="content"></div>
 var source = new EventSource('/talk');
 source.addEventListener('open', function(e) {
 document.getElementById('content').innerHTML += 'Connections to the server established..<br/>';
 }, false);
 source.onmessage = function(e) {
 document.getElementById('content').innerHTML += + '<br/>';
 document.getElementById('content').innerHTML += 'Listening to server events stopped..<br/>';

The Server Implementation

Now that we’ve covered the client side we’re in need of a server that propagates events periodically to the client.

I have added two example implementations here – one that is programmed in javascript to be run with node.js and another implementation as classical java servlet using Jetty as web container.

Using Node.js

To run the demo application using node.js simply run the following command from the project’s root directory:

node server.js

Here is the server implementation in Javascript:

var http = require('http');
var fs = require('fs');
 * send interval in millis
var sendInterval = 5000;
function sendServerSendEvent(req, res) {
 res.writeHead(200, {
 'Content-Type' : 'text/event-stream',
 'Cache-Control' : 'no-cache',
 'Connection' : 'keep-alive'
 var sseId = (new Date()).toLocaleTimeString();
 setInterval(function() {
 writeServerSendEvent(res, sseId, (new Date()).toLocaleTimeString());
 }, sendInterval);
 writeServerSendEvent(res, sseId, (new Date()).toLocaleTimeString());
function writeServerSendEvent(res, sseId, data) {
 res.write('id: ' + sseId + '\n');
 res.write("data: new server event " + data + '\n\n');
http.createServer(function(req, res) {
 if (req.headers.accept && req.headers.accept == 'text/event-stream') {
 if (req.url == '/talk') {
 sendServerSendEvent(req, res);
 } else {
 } else {
 res.writeHead(200, {
 'Content-Type' : 'text/html'
 res.write(fs.readFileSync(__dirname + '/index.html'));

Afterwards you should be able to open http://localhost:8080 in your browser and watch the events. Pressing the button should stop listening to the server send events.

Receiving HTML5 Server-Send Events in Chrome

Receiving HTML5 Server-Send Events in Chrome

Using Jetty

I am lazy that’s why I have used the Jetty EventSourceServlet (to be found in this GitHub project) so I don’t have to implement the HTTP states, content type negotiation etc ..

That is what my quick solution looks like:

package com.hascode.tutorial;
import java.util.Date;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.servlets.EventSource;
import org.eclipse.jetty.servlets.EventSourceServlet;
@WebServlet(urlPatterns = "/talk", initParams = { @WebInitParam(name = "heartBeatPeriod", value = "5") }, asyncSupported = true)
public class MySSEServlet extends EventSourceServlet {
 private static final long serialVersionUID = 1L;
 protected EventSource newEventSource(final HttpServletRequest req) {
 return new EventSource() {
 public void onOpen(final Emitter emitter) throws IOException {"new server event " + new Date().toString());
 while (true) {
 System.out.println("propagating event..");
 try {
 Thread.sleep(5000);"new server event "
 + new Date().toString());
 } catch (InterruptedException e) {
 public void onClose() {

For convenience, I have added the following web.xml to load the index.html as start page:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

To start the server instance simply run the following command:

mvn jetty:run

Afterwards you should be able to see a similar result in your browser when pointing it to http://localhost:8080

Receiving SSEs in Chrome with a Jetty Server

Receiving SSEs in Chrome with a Jetty Server

Tutorial Sources

Please feel free to download the tutorial sources from my Bitbucket repository, fork it there or clone it using Mercurial:

hg clone


Tags: , , , , , , , , ,

One Response to “HTML5 Server Send Events using Node.js or Jetty”

  1. Anu Says:

    Hi thanks for sharing very useful information for beginners like me. I have one question how to run the cli commands when button is clicked using node js.

Leave a Reply

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 113,818 bad guys.