Skip to main content

Server Sent Events Sample with Glassfish

Posted by bhaktimehta on April 21, 2012 at 12:34 AM PDT


Server-Sent Events (SSE) is a standard describing how servers can initiate data transmission towards clients once an initial client connection has been established.
It is commonly used to send message updates or continuous data streams to a browser client and designed to enhance native, cross-browser streaming through a JavaScript API called EventSource, through which a client requests a particular URL in order to receive an event stream.


The sample demonstrates a simple example of SSE. Here we have a servlet which periodically polls for Twitter information and pushes the data to the clients using Server Sent Events. This samples showcases a JavaEE6 application that uses CDI,ServerSentEventHandler and ServerSentEventHandlerContext apis along with twitter search apis and javascript client code. 

Code Snippets

Here is the code for the ServletContextInitializer. There is a Timer which gets the feeds periodically

public class SSEServletContextListener implements ServletContextListener {

    TwitterFeedBean tfs;

    private final int time = 60;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
            // create the timer and timer task objects
            Timer timer = new Timer();

            TwitterTimerTask task = new TwitterTimerTask(tfs);

            timer.schedule(task, 0, 1000 * time);

            // save our timer for later use
            servletContext.setAttribute ("timer", timer);
        } catch (Exception e) {
            servletContext.log ("Problem initializing timer task every "+ time +" seconds: "
                    + e.getMessage ());

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();

        Timer timer = (Timer)servletContext.getAttribute ("timer");

        if (timer != null)

        servletContext.removeAttribute ("timer");

    class TwitterTimerTask extends TimerTask {
        TwitterFeedBean bean;
        TwitterTimerTask(TwitterFeedBean  t)   {
            this.bean = t;

        public void run() {
            try {
            } catch (IOException e) {


Here is the TwitterHandler which is a ServerSentEventHandler

* TwitterHandler class which will send messages using Server Sent Events
* @author Bhakti Mehta
public class TwitterHandler extends ServerSentEventHandler {

    public void onConnected(ServerSentEventConnection client) {


    public void sendMessage(String data) {
        System.out.println("Handler="+this+" Sending data="+data);
        try {
        } catch(IOException ioe) {
            // May be client is already disconnected. Just close it
            try {
            } catch(Exception e) {
                //ignore if the connection close threw an exception


Here is the TwitterFeedBean which gets the data from the Twitter search url
public class TwitterFeedBean {

    @Inject @ServerSentEventContext("/twittersse")
    ServerSentEventHandlerContext sc;

    final private String SEARCH_URL =
            "" +

    public TwitterFeedBean() {

    public void getFeeds() throws IOException {

        for(TwitterHandler handler : sc.getHandlers()) {


    private  String composeMessage() {
        return getFeedData();

    private  String getFeedData() {
        StringBuilder sb = new StringBuilder();
        try {
            URL twitter = new URL(SEARCH_URL);
            URLConnection yc = null;

            yc = twitter.openConnection();

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                System.out.println("Input line" + inputLine);
                sb.append(inputLine)  ;


            return sb.toString();
        } catch (IOException e) {
        return new String("Error in getting data from the feeds ") ;


Finally here is the app.js which will create the EventSource. It also parses the JSON feeds

var network = function (eventSource) {
    return {
        initialize: function() {
            var url = 'http://' +
                + '/sse-twitter-sample/twittersse';
            eventSource = new EventSource(url);

            eventSource.onmessage = function (event) {
                var feeds =;
                var theNewParagraph = document.createElement('p');

                var theBreak = document.createElement('br');
                theNewParagraph.setAttribute('title','The feeds');
                var data = JSON.parse(feeds);
                var theText;

                var divTag ;
                var tweetInfo  ;

                // Before we continue we check that we got data
                if(data !== undefined) {
                    for (var k = 0;k<5;k++) {
                        tweetInfo = data.results[k];

                        divTag = document.createElement("div");
               = "div"+k;
               = "0px auto";
                        divTag.className ="outerDiv";

                        divTag.innerHTML =
                            ''+ tweetInfo.text+'<\/a>'+

                                 ' Created on '  + tweetInfo.created_at  ;




        send: function(commandn) {

var APP = {
    network: network(null),

    initialize: function () {;

window.onload = APP.initialize;

To run the sample deploy the sse-twitter-sample.war to the latest nightly of glassfish 4.0 and you can run
http://localhost:8080/sse-twitter-sample/ You may have to wait for a minute before the feeds start appearing. Also since this is a sample to stop the timer you should undeploy the application otherwise it will keep getting the tweets..
Jitu has another sample here
Note this is a work in progress so apis may change.

sse-twitter-sample.zip18.65 KB
Related Topics >>