Monday, July 4, 2011

HTML5 WebSockets for JVM Developers

Anything related around HTML5 seems to be a hot button topic these days and lucky for me that I got an opportunity to take a deep dive into WebSockets. Thus, I will use this blog to post my findings over the next few days and I will present some simple code examples as well.

Anyway, WebSockets allow Servers to basically "push" data back to subscribed Browser clients in an effective manner instead of using workarounds such as long-polling. The WebSockets API is being standardized by the W3C ( and the actual protocol is being defined by the Hypertext-Bidirectional (HyBi) working group (Part of IETF) -

For further reading and to learn more about WebSockets in general, please also check out the following resources:
It turns out, though, that the only browsers, that support WebSockets out of the box right now, are Safari and Chrome. Wait - Firefox is not supported? Well, Firefox does support WebSockets but it is disabled by default due to security issues with the implemented draft version of the WebSocket protocol (Firefox 4+5). You can find more information about this here:
Nonetheless, if you're interested, you can re-activate WebSockets in Firefox 4 and 5 quite easily. Take a look at the following link for more information on how to do this:
Firefox 6 is supposed to bring back websockets support, albeit using a newer draft of the websocket protocol (hybi-07), which is not backwards compatible.

What about Web Servers?

Okay, so WebSockets look cool, yet with limited browser support. Furthermore, different webservers have varying stages of support for asynchronous messaging, with Jetty and Glassfish being the best options currently a provide native websocket support. It looks like Resin also has direct websocket support. See also:
Tomcat 7 currently does not support WebSockets, yet. Check out the following issue tracker entry to learn more about the current state of affairs in Tomcat 7:
What's next?

So, essentially what we want is some kind of framework that supports "graceful degradation": when available a suitable framework shall use websockets, but if not available, the framework should fall back to more traditional push work-arounds such as long-polling.

On the server-side, I am running Java, and therefore, I need a respective implementation on that platform. Choices, though, seem to be rather limited and I came up with the following list of options:
Just for reference, there seems to be quite some activity going on outside of Java:
My choice for now

Socket.IO looks like it has quite some traction but the default implementation is for Node.JS. There are Java implementations available but they did not seem to have much traction. jWebSocket actually looks promising but its licensing (LGPL) is too restrictive to me (My choice needs to be Apache license compatible ;-).

Out of the list above, I chose Atmosphere ( Atmosphere has pretty much all the features you would like to have incl. detecting server-side capabilities and using the best possible approach transparently. Atmosphere comes with a jQuery plugin that abstracts away the implementation details (same Api for using long-polling vs. websockets)

While Atmosphere comes with quite a few examples, they are interestingly mostly for JAX-RS and there are no examples for Spring MVC. Also, the documentations is rather limited but luckily you get very swift and helpful responses from Atmosphere mailing list.

In my next posting, I will provide a little example integrating Spring MVC and Atmosphere. So stay tuned.

Nevertheless, if you have other WebSocket recommendations and/or thoughts, please leave comments!


A funny thing, I just realized that Burr Sutter, a friend of mine and fellow AJUG board member wrote down his WebSockets research experiences as well. It is a small world. Go check it out at:


Ayush Gupta said...

Play Framework, a java based framework is pretty good in general also also has great support for websockets

Anonymous said...

Waiting for your post about the integration with Spring :)

Gurkan Erdogdu said...

Siwpas 2.0.0 will support WebSocket. You can check siwpas from