DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > HTML5 WebSockets for JVM Developers

HTML5 WebSockets for JVM Developers

Gunnar Hillert user avatar by
Gunnar Hillert
·
Dec. 05, 11 · Java Zone · Interview
Like (0)
Save
Tweet
15.12K Views

Join the DZone community and get the full member experience.

Join For Free

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 (http://dev.w3.org/html5/websockets/) and the actual protocol is being defined by the Hypertext-Bidirectional (HyBi) working group (Part of IETF) - http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-09.

For further reading and to learn more about WebSockets in general, please also check out the following resources:

  • http://www.html5rocks.com/en/tutorials/websockets/basics/
  • http://websocket.org/
  • https://developer.mozilla.org/en/WebSockets
  • http://en.wikipedia.org/wiki/WebSockets
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:
  • http://hacks.mozilla.org/2010/12/websockets-disabled-in-firefox-4/
  • https://developer.mozilla.org/en/WebSockets
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:
  • http://techdows.com/2010/12/turn-on-websockets-in-firefox-4.html
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:
  • http://java.dzone.com/articles/creating-websocket-chat
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:
  • https://issues.apache.org/bugzilla/show_bug.cgi?id=51181
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:
  • Atmosphere (http://atmosphere.java.net/)
  • Socket.IO (http://socket.io/) - Orginally for Node.JS there are Java implementations available
  • jWebSocket (http://jwebsocket.org/)
  • Stomp Websocket for HornetQ and ActiveMQ (http://jmesnil.net/stomp-websocket/doc/)
Just for reference, there seems to be quite some activity going on outside of Java:
  • https://github.com/maccman/juggernaut
  • https://github.com/socky/socky-server-ruby
  • https://github.com/igrigorik/em-websocket
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 (http://atmosphere.java.net/). 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!

PS:

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:
  • http://burrsutter.blogspot.com/2011/02/html5-websockets-adventure.html

 

From http://hillert.blogspot.com/2011/07/html5-websockets-for-jvm-developers.html

Java (programming language) WebSocket HTML Java virtual machine

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022
  • Why Is Software Integration Important for Business?
  • How Database B-Tree Indexing Works
  • Maven Tutorial: Nice and Easy [Video]

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo