How the WebSocket Implementation Works in Apache Tomcat 7


News: How the WebSocket Implementation Works in Apache Tomcat 7

  1. Following an article last week on what a WebSocket implementation will do for Apache Tomcat 7, VMware SpringSource engineer and the Apache Software Foundation (ASF) lead for the Apache Tomcat 7 project, Filip Hanik shared some of the rationale behind how the Tomcat team implemented WebSocket in the recent Tomcat 7.0.27 release. Hanik describes how the Tomcat team implemented it with the following:

    Since WebSocket is a protocol sent over TCP after an initial HTTP handshake, you could effectively implement WebSocket using Tomcat’s Comet implementation. There is a back port to Tomcat 6 suggested that does exactly that with very minor changes.

    The Apache Tomcat team however decided to go with a more substantial implementation with changes to the core of Tomcat’s network and protocol implementation. The reason for this was memory and scalability based. If Tomcat can recycle the HttpServletRequest/Response objects after the initial handshake, each WebSocket connection will take up less memory in the Java heap. It also opens up the Tomcat container for other future protocols that utilize the HTTP Upgrade feature.

    WebSocket allows developers to essentially do only two things: send and receive messages. This means users have to implement a mechanism to send a message based on a specific action in their application and implement a callback listener when a message arrives back from a client. Hanik walks through a detailed Chat example that can be found in the webapps/examples/websocket folder of the Apache Tomcat installation.

    He breaks it down into 4 simple steps. Tto see the full article see How Apache Implemented WebSocket on

  2. This is a Tomcat specific implementation and is not based on (the incomplete) JSR356 as far as I can tell. Jetty also supports WebSockets and has its own implemention quite different from Tomcat's.