WebSockets in Tomcat 7

posted by fhanik on April 23, 2012 10:22 PM

With the 7.0.27 release the Apache Tomcat team introduced a WebSocket implementation. WebSocket has received a lot of hype, and has been much anticipated by Tomcat users. Let’s take a quick look at what web sockets are, what benefits and limitations they have and how they are implemented in Apache Tomcat 7.

What is a WebSocket?

WebSocket is considered the next step in evolution of web communication. Over time, communication has evolved in steps to reduce the time and data throughput for the application to update a user’s browser. The evolution has looked a little like this:

  • Entire page reloads
  • Component reloads using AJAX Processing
  • Comet communication
    • Long poll– similar to AJAX, but not holding a thread on the server
    • Bi directional- two way communication over the same TCP

Each of these steps had their benefits and challenges. Apache Tomcat 6 implements bi-directional communication over HTTP using its Comet Processor. This implementation allowed for asynchronous event driven request processing as well as bi-directional communication. This implementation had a few limitations.

  1. Given that HTTP was intended to be request/response protocol rather than a bi directional protocol, proxies and other intermediaries may not work properly, and only forward packets in one direction at any given time.
  2. The programming model becomes more difficult as this introduces multi threading to the servlet developer
  3. Non-standard APIs made adoption difficult

Comet proved that the proverb “build it and they will come” is not necessarily true. Each major servlet container had its own implementation and non-standard APIs. Adoption was fairly sporadic.

The Servlet 3.0 release introduced a new feature called Asynchronous Servlets. This implementation standardized and simplified most Comet implementations by removing the bi-directional requirement of the protocol but retaining the ability to disassociate a container thread from the HTTP request. We can compare this feature to the server version of client AJAX calls. It lets a request hang until a response is ready to be delivered without using worker thread on the container. This feature offers little new over Apache Tomcat’s Comet Processor in terms of functionality. The fact that it is a standard Servlet API, gives frameworks an easier way to build on top of it.

Asynchronous servlets address some of the need in web communication, but not all, and the lack of bi-directional communication made its use still limited. If you take standard API aside, it may have been considered step back from most Comet implementations.

WebSockets is another attempt to standardize an idea of having asynchronous, event driven and bi-directional communication over HTTP. The standardization comes in form of a JavaScript API as well as a communication protocol. While this highlights the lack of a standard server-side API the Servlet 3.1 expert group are already discussing including some basic level of support for this and discussions are active as we are writing this article. Whether that will evolve into a full WebSocket API remains to be seen. In the meantime major servlet container are publishing non- standard APIs, Tomcat being no different.

So let’s summarize what a WebSocket implementation gives us:

  1. Bi-directional communication over a HTTP port by upgrading/switching protocols. Initiated by a HTTP request.
  2. Message/Frame based communication
  3. Intended to work with proxies and intermediaries

What does it look Like?

On the protocol level there is one major difference from what has been done previously, and that is that the WebSocket switches the protocol away from HTTP. This is where a proxy can change its behavior and support this type of messaging as well. Whether this is in theory only still remains to be seen. If we look at a WebSocket client initiating a request we can see that the intermediary has to, at a minimum,

GET /chat HTTP/1.1
Upgrade: websocket
Connection: Upgrade

Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13

understand the “Connection:upgrade” headers. The response contains the critical status code of 101

HTTP/1.1 101
Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

indicating that a protocol switch has been confirmed and is now expected. After this handshake is complete the client and server move away from request/response communication and can send messages independently of each other.

Does the WebSocket protocol address all the limitations of previous Comet implementations?

In short: not completely. First, proxies and intermediaries may still not work – so deployments using Wide Area Networks (WANs) are questionable. Secondly, there are still no standard Java APIs. However, given the buzz around WebSocket, this may be addressed soon.

Another thing to be careful for is that every HTTP/TCP connection requires the initial handshake and introduces a message roundtrip before you even send or receive data over the WebSocket protocol. If you’re not careful, and have very short interactions, WebSocket may be too much overhead. WebSocket are ideal for longer running conversations between client and server.

The good news is that WebSocket has benefited from a lot more attention, especially client side and cross language than any other previous attempt at enhancing web communication. It has also received a competitor for bi-directional communication, and that is SPDY, an implementation also underway within the Apache Tomcat project.


WebSocket is a straightforward implementation of bi-directional communication over HTTP. The API is part of the HTML 5 specification and so far many different server side implementations are available. It is not without risks, since the Java APIs are yet to be standardized so implementations can vary. Additionally, working with proxies and intermediaries may still not work, however it is important to note that WebSocket utilizes the Upgrade feature of HTTP to switch protocols after the initial request/response handshake has been completed. This gives future proxies an option to reject or support the WebSocket protocol over WAN.

The first released version of WebSocket in Apache Tomcat was released with Apache Tomcat 7.0.27.

Filip Hanik is a Senior Software Engineer for the SpringSource Division of VMware, Inc. (NYSE: VMW) and a key participant in the company's Apache Tomcat initiatives. Filip brings 15 years of extensive experience in architecture, design and development of distributed application frameworks and containers and is recognized for his top-quality system development skills and continuous participation of Open Source development projects. Filip is an Apache Software Foundation member and a committer to the Apache Tomcat project where he is a leading authority on Tomcat clustering and a key contributor to the core of the platform. Filip has made contributions to software initiatives for, Sony Music, France Telecom and has held a variety of senior software engineering positions with technology companies in both the United States and Sweden. He received his education at Chalmers University of Technology in Gothenburg, Sweden where he majored in Computer Science and Computer Engineering.


Tomcat maven plugin for version 7.0.27

Hello Filip,

I wonder where to download a maven tomcat plugin that works with the referred version.

BTW, awesome functionality delivered by the Tomcat team guys.

jD @

This is an awesome post. Just

This is an awesome post. Just one of the very best post I’ve ever seen. What a really good and awesome post. Keep up your work on articles! Power Qube - OneSolar

I appreciate everything you

I appreciate everything you have added to my knowledge base. Admiring the time and effort you put into your blog and detailed information you offer. Thanks. 3kw solar system - VicSolar

threading with websockets

Thanks for the article. From a quick look at a thread dump a websocket servlet on a NIO connector with an open connection seems to occupy an executor thread for the life of the connection?
Seems like an obvious scalability problem?
Also the connections seem to be closed automatically on some sort of timeout, do you know what the configuration value for that is?

The stack of the occupied thread is from a thread dump it (note its an exec thread):

"http-nio-8443-exec-14" daemon prio=5 tid=7f9b571ec000 nid=0x119dab000 waiting on condition [119daa000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <7b94fd1b0> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.parkNanos(
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(
at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(
at java.util.concurrent.CountDownLatch.await(
at org.apache.coyote.http11.upgrade.UpgradeNioProcessor.readSocket(
at org.apache.catalina.websocket.WsFrame.nextFrame(
at org.apache.catalina.websocket.WsInputStream.nextFrame(
at org.apache.catalina.websocket.StreamInbound.onData(
at org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
- locked <7b8111018> (a
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
at java.util.concurrent.ThreadPoolExecutor$

blocked threads

I see a similar stack with websockets holding threads open on 7.0.30 and 7.0.39. This could be a real problem

Name: http-bio-4415-exec-5
Total blocked: 50 Total waited: 112

Stack trace: Method)
- locked


8 ball pool Acknowledges for penmanship such a worthy column, I stumbled beside your blog besides predict a handful advise. I want your tone of manuscript... coc hack

Just keep writing what you are excited about

Just keep writing what you are excited about, and the visitors will come to your post. Situs Kesehatan Veherba
obat kanker payudara herbal
obat penyakit stroke

After checking out a number

After checking out a number of the articles on your site, I honestly appreciate your technique of writing a blog. I saved as a favorite it to my bookmark webpage list and will be checking back in the near future. Please visit my website as well and let me know your opinion.
Mortgage Broker Calgary

To take the full time to talk

To take the full time to talk about around see post, exemplary info on your site, thanks. Incredible understanding you've with this, it is good to locate a site that details information that is so much about designers that are various.

I use basically superior

I use basically superior fabrics : you will discover these products by: seguridad privada

Actually I read it yesterday

Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written. Love Traction Lines Review

Fat Diminisher System | Testerone XL Review

testosterone xl I propose merely very good along with reputable data, consequently visualize it:

Till they've been waste

Till they've been waste collected nevertheless, when the software is reloaded film making, the present available contacts within the previous swimming wouldn't shut.

Prima Doula

Find the best essays on Postpartum Doula Support Calgary is my friend's profile page.

Pak Readers

I encourage you to read this text it is fun Books described ...

Top Cool Websites

On this subject internet page, you'll see my best information, be sure to look over this level of detail.
top websites

Budget Box Guy Ltd

Very interesting information, worth recommending. However, I recommend this:
bedding stores calgary


It is especially decent, though look into the tips during this home address.
instagram for pc

I actually enjoy your manner

I actually enjoy your manner of composing a website after looking at numerous the posts in your website such a good point. I you will check in the long run and saved like a preferred it to my bookmark website checklist. Please visit with my site aswell and I would like to understand your view.


gives frameworks an easier way to build on top of coach atlanta


If you don't mind share more like that. stolen identity


You will find there's means anyone produce your current websites that creates people preserve ever coming back. It’s how we employ your current words and phrases for you to communicate the details.
Pro DJ

It is and so wonderful in

It is and so wonderful in addition to resourceful. I just now appreciate this colorings in addition to the person who obtains the item from the send will likely be happy.
Jual PC HP

This is a great inspiring

This is a great inspiring article.I am pretty much pleased with your good work. pvc vloeren vriezenveen

After reading your article I

After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. the respect principle reviews


Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know.
geometry dash| sniper games |happy wheels | happy wheels 2 |agario| my little pony games mickey mouse games

Very interesting website what

Very interesting website what Google did to me. Iam most definately thinking about that one, although a lot of sites I observe nowadays do not actually supply something that Iam thinking about. Simply thought that I let and would publish you know.

Looking at your content I was

Looking at your content I was surprised find this. I know that you describe it very well. And Hopefully other visitors will also experience how I experience after studying your content. I really experience thankful that I just look at this. It is very useful and very useful and I really many userful factors here from it.

Health Insurance in

Health Insurance in Singapore I am cheerful to discover this post Very valuable for me, as it contains parcel of data. I Always want to peruse The Quality and happy I discovered this thing in you post. Much obliged bupa international singapore

Websites Moving Problem

After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. I feel very grateful that I read this.

roblox robux generator | Hack Agario | Pixel Gun 3D Cheats

You could be allowed to put

You could be allowed to put up manufacturers, except for shortcuts, in the event that they've been recognized not to mention concerning content.
Cheap ClubFlyers

API remains to be seen. In

API remains to be seen. In the meantime major servlet container are publishing non- standard APIs, Tomcat being no different.cccam server

Looking at your material I

Looking at your material I was amazed discover this. I know that you explain it very well agree with. And Hopefully other guests will also encounter how I encounter after learning your material. I really encounter grateful that I just look at this. It is very useful and very useful and I really many userful aspects here from it.

Hi. I discovered your web

Hi. I discovered your web journal utilizing msn. This is a to a great degree elegantly composed article. I will make sure to bookmark it and come back to peruse a greater amount of your valuable data. A debt of gratitude is in order for the post. I"ll surely rebound. go here

Much thanks for this

Much thanks for this incredible post. Foreclosure

I wanted to thank you

I wanted to thank you for this excellent read. I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post.


It's difficult to get learning RUSSIAN in blogging world, but your tips are so helpful. Looks like I've got some work ahead of me! :) Thanks for the great read!firefly tv box wintel box


Admiring the time and effort you put into your blog,thanks.mxq plus firmware
wintel w8 pro

such an amazing information

such an amazing information
كلاش اوف كلانس
لعبة المزرعة السعيدة

An intriguing discussion is

An intriguing discussion is worth comment. There's no doubt that that you ought to publish more about this subject matter, it might not be a taboo subject but usually people do not speak about such subjects. To the next! Many thanks!!
Edmonton Mortgage Broker
Life Insurance Vancouver
Life Insurance Calgary


Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
five nights at freddy's 3 five nights at freddy's 4 five nights at freddy's 2 five nights at freddy's

An interesting dialogue may

An interesting dialogue may be worth remark. Often although there is no question that that you simply must submit more about that subject material buy kids furniture, it could not be considered a taboo topic individuals don't talk about such topics. To another! Thank you!!

I truly value this superb

I truly value this superb post that you have accommodated us. I guarantee this would be helpful for a large portion of the general population. seo company at sg

Good to become visiting with

Good to become visiting with your site again, it's been weeks for me personally. Well this informative article that I have been waited for such a long time Visit This Link. I want my task to be completed by this article within the university, and contains same subject together with your post. Excellent share, cheers.

Awesome to be going to your

Awesome to be going to your weblog site again, it has been several weeks for me. Well this post that i've been patiently waited for so lengthy blog here . I need this post to finish my task in the higher education, and it has same subject with your content. Thanks, excellent discuss.

best stock trading system

One of the many blog sites using the net, I like perusing joining your the foremost; most people make available you and me such a lot of material within smallish section. I'm sure on the lookout for not to mention as there are a spirit for the purpose of helpful penning, I just determined you would purpose everybody in your best suited place. How can you guidance? best stock trading system


This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. mahjong

Among the several sites that

Among the several sites that are blog using the web, I love reading joining your the foremost people offer me and you such a great deal of substance within area that is smallish. I am positive looking for and of course as certainly a nature are with the objective of useful writing link, I simply decided everyone would be purposed by you inside your ideal location. How will you assistance?


I truly welcome the sort of themes you post here. A debt of gratitude is in order for sharing us an extraordinary data that is really useful. Great day! Allianz Insurance

OC Housing News

Such sites are important because they provide a large dose of useful information ...
Housing Market News

Post new comment

This question is for testing whether you are a human visitor and to prevent automated spam submissions.