Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 1 | <!DOCTYPE html SYSTEM "about:legacy-compat"> |
| 2 | <html lang="en"><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><link href="./images/docs-stylesheet.css" rel="stylesheet" type="text/css"><title>Apache Tomcat 9 (9.0.112) - WebSocket How-To</title></head><body><div id="wrapper"><header><div id="header"><div><div><div class="logo noPrint"><a href="https://tomcat.apache.org/"><img alt="Tomcat Home" src="./images/tomcat.png"></a></div><div style="height: 1px;"></div><div class="asfLogo noPrint"><a href="https://www.apache.org/" target="_blank"><img src="./images/asf-logo.svg" alt="The Apache Software Foundation" style="width: 266px; height: 83px;"></a></div><h1>Apache Tomcat 9</h1><div class="versionInfo"> |
||
| 3 | Version 9.0.112, |
||
| 4 | <time datetime="2025-11-06">Nov 6 2025</time></div><div style="height: 1px;"></div><div style="clear: left;"></div></div></div></div></header><div id="middle"><div><div id="mainLeft" class="noprint"><div><nav><div><h2>Links</h2><ul><li><a href="index.html">Docs Home</a></li><li><a href="https://cwiki.apache.org/confluence/display/TOMCAT/FAQ">FAQ</a></li></ul></div><div><h2>User Guide</h2><ul><li><a href="introduction.html">1) Introduction</a></li><li><a href="setup.html">2) Setup</a></li><li><a href="appdev/index.html">3) First webapp</a></li><li><a href="deployer-howto.html">4) Deployer</a></li><li><a href="manager-howto.html">5) Manager</a></li><li><a href="host-manager-howto.html">6) Host Manager</a></li><li><a href="realm-howto.html">7) Realms and AAA</a></li><li><a href="security-manager-howto.html">8) Security Manager</a></li><li><a href="jndi-resources-howto.html">9) JNDI Resources</a></li><li><a href="jndi-datasource-examples-howto.html">10) JDBC DataSources</a></li><li><a href="class-loader-howto.html">11) Classloading</a></li><li><a href="jasper-howto.html">12) JSPs</a></li><li><a href="ssl-howto.html">13) SSL/TLS</a></li><li><a href="ssi-howto.html">14) SSI</a></li><li><a href="cgi-howto.html">15) CGI</a></li><li><a href="proxy-howto.html">16) Proxy Support</a></li><li><a href="mbeans-descriptors-howto.html">17) MBeans Descriptors</a></li><li><a href="default-servlet.html">18) Default Servlet</a></li><li><a href="cluster-howto.html">19) Clustering</a></li><li><a href="balancer-howto.html">20) Load Balancer</a></li><li><a href="connectors.html">21) Connectors</a></li><li><a href="monitoring.html">22) Monitoring and Management</a></li><li><a href="logging.html">23) Logging</a></li><li><a href="apr.html">24) APR/Native</a></li><li><a href="virtual-hosting-howto.html">25) Virtual Hosting</a></li><li><a href="aio.html">26) Advanced IO</a></li><li><a href="maven-jars.html">27) Mavenized</a></li><li><a href="security-howto.html">28) Security Considerations</a></li><li><a href="windows-service-howto.html">29) Windows Service</a></li><li><a href="windows-auth-howto.html">30) Windows Authentication</a></li><li><a href="jdbc-pool.html">31) Tomcat's JDBC Pool</a></li><li><a href="web-socket-howto.html">32) WebSocket</a></li><li><a href="rewrite.html">33) Rewrite</a></li><li><a href="cdi.html">34) CDI 2 and JAX-RS</a></li><li><a href="graal.html">35) AOT/GraalVM Support</a></li></ul></div><div><h2>Reference</h2><ul><li><a href="RELEASE-NOTES.txt">Release Notes</a></li><li><a href="config/index.html">Configuration</a></li><li><a href="api/index.html">Tomcat Javadocs</a></li><li><a href="servletapi/index.html">Servlet 4.0 Javadocs</a></li><li><a href="jspapi/index.html">JSP 2.3 Javadocs</a></li><li><a href="elapi/index.html">EL 3.0 Javadocs</a></li><li><a href="websocketapi/index.html">WebSocket 1.1 Javadocs</a></li><li><a href="jaspicapi/index.html">JASPIC 1.1 Javadocs</a></li><li><a href="annotationapi/index.html">Common Annotations 1.3 Javadocs</a></li><li><a href="https://tomcat.apache.org/connectors-doc/">JK 1.2 Documentation</a></li></ul></div><div><h2>Apache Tomcat Development</h2><ul><li><a href="building.html">Building</a></li><li><a href="changelog.html">Changelog</a></li><li><a href="https://cwiki.apache.org/confluence/display/TOMCAT/Tomcat+Versions">Status</a></li><li><a href="developers.html">Developers</a></li><li><a href="architecture/index.html">Architecture</a></li><li><a href="tribes/introduction.html">Tribes</a></li></ul></div></nav></div></div><div id="mainRight"><div id="content"><h2>WebSocket How-To</h2><h3 id="Table_of_Contents">Table of Contents</h3><div class="text"> |
||
| 5 | <ul><li><a href="#Overview">Overview</a></li><li><a href="#Application_development">Application development</a></li><li><a href="#Tomcat_WebSocket_specific_configuration">Tomcat WebSocket specific configuration</a></li></ul> |
||
| 6 | </div><h3 id="Overview">Overview</h3><div class="text"> |
||
| 7 | <p>Tomcat provides support for WebSocket as defined by |
||
| 8 | <a href="https://tools.ietf.org/html/rfc6455">RFC 6455</a>.</p> |
||
| 9 | </div><h3 id="Application_development">Application development</h3><div class="text"> |
||
| 10 | <p>Tomcat implements the Java WebSocket 1.1 API defined by <a href="https://www.jcp.org/en/jsr/detail?id=356">JSR-356</a>.</p> |
||
| 11 | |||
| 12 | <p>There are several example applications that demonstrate how the WebSocket API |
||
| 13 | can be used. You will need to look at both the client side <a href="https://github.com/apache/tomcat/tree/9.0.x/webapps/examples/websocket"> |
||
| 14 | HTML</a> and the server side <a href="https://github.com/apache/tomcat/tree/9.0.x/webapps/examples/WEB-INF/classes/websocket"> |
||
| 15 | code</a>.</p> |
||
| 16 | </div><h3 id="Tomcat_WebSocket_specific_configuration">Tomcat WebSocket specific configuration</h3><div class="text"> |
||
| 17 | |||
| 18 | <p>Tomcat provides a number of Tomcat specific configuration options for |
||
| 19 | WebSocket. It is anticipated that these will be absorbed into the WebSocket |
||
| 20 | specification over time.</p> |
||
| 21 | |||
| 22 | <p>The write timeout used when sending WebSocket messages in blocking mode |
||
| 23 | defaults to 20000 milliseconds (20 seconds). This may be changed by setting |
||
| 24 | the property <code>org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT</code> |
||
| 25 | in the user properties collection attached to the WebSocket session. The |
||
| 26 | value assigned to this property should be a <code>Long</code> and represents |
||
| 27 | the timeout to use in milliseconds. For an infinite timeout, use |
||
| 28 | <code>-1</code>.</p> |
||
| 29 | |||
| 30 | <p>The time Tomcat waits for a peer to send a WebSocket session close message |
||
| 31 | after Tomcat has sent a close message to the peer defaults to 30000 |
||
| 32 | milliseconds (30 seconds). This may be changed by setting the property |
||
| 33 | <code>org.apache.tomcat.websocket.SESSION_CLOSE_TIMEOUT</code> in the user |
||
| 34 | properties collection attached to the WebSocket session. The value assigned |
||
| 35 | to this property should be a <code>Long</code> and represents the timeout to |
||
| 36 | use in milliseconds. Values less than or equal to zero will be ignored.</p> |
||
| 37 | |||
| 38 | <p>The write timeout Tomcat uses when writing a session close message when the |
||
| 39 | close is abnormal defaults to 50 milliseconds. This may be changed by setting |
||
| 40 | the property |
||
| 41 | <code>org.apache.tomcat.websocket.ABNORMAL_SESSION_CLOSE_SEND_TIMEOUT</code> |
||
| 42 | in the user properties collection attached to the WebSocket session. The |
||
| 43 | value assigned to this property should be a <code>Long</code> and represents |
||
| 44 | the timeout to use in milliseconds. Values less than or equal to zero will be |
||
| 45 | ignored.</p> |
||
| 46 | |||
| 47 | <p>In addition to the <code>Session.setMaxIdleTimeout(long)</code> method which |
||
| 48 | is part of the Java WebSocket API, Tomcat provides greater control of the |
||
| 49 | timing out the session due to lack of activity. Setting the property |
||
| 50 | <code>org.apache.tomcat.websocket.READ_IDLE_TIMEOUT_MS</code> in the user |
||
| 51 | properties collection attached to the WebSocket session will trigger a |
||
| 52 | session timeout if no WebSocket message is received for the specified number |
||
| 53 | of milliseconds. Setting the property |
||
| 54 | <code>org.apache.tomcat.websocket.WRITE_IDLE_TIMEOUT_MS</code> will trigger a |
||
| 55 | session timeout if no WebSocket message is sent for the specified number of |
||
| 56 | milliseconds. These can be used separately or together, with or without |
||
| 57 | <code>Session.setMaxIdleTimeout(long)</code>. If the associated property is |
||
| 58 | not specified, the read and/or write idle timeout will be applied.</p> |
||
| 59 | |||
| 60 | <p>If the application does not define a <code>MessageHandler.Partial</code> for |
||
| 61 | incoming binary messages, any incoming binary messages must be buffered so |
||
| 62 | the entire message can be delivered in a single call to the registered |
||
| 63 | <code>MessageHandler.Whole</code> for binary messages. The default buffer |
||
| 64 | size for binary messages is 8192 bytes. This may be changed for a web |
||
| 65 | application by setting the servlet context initialization parameter |
||
| 66 | <code>org.apache.tomcat.websocket.binaryBufferSize</code> to the desired |
||
| 67 | value in bytes.</p> |
||
| 68 | |||
| 69 | <p>If the application does not define a <code>MessageHandler.Partial</code> for |
||
| 70 | incoming text messages, any incoming text messages must be buffered so the |
||
| 71 | entire message can be delivered in a single call to the registered |
||
| 72 | <code>MessageHandler.Whole</code> for text messages. The default buffer size |
||
| 73 | for text messages is 8192 bytes. This may be changed for a web application by |
||
| 74 | setting the servlet context initialization parameter |
||
| 75 | <code>org.apache.tomcat.websocket.textBufferSize</code> to the desired value |
||
| 76 | in bytes.</p> |
||
| 77 | |||
| 78 | <p>The Java WebSocket specification 1.0 does not permit programmatic deployment |
||
| 79 | after the first endpoint has started a WebSocket handshake. By default, |
||
| 80 | Tomcat continues to permit additional programmatic deployment. This |
||
| 81 | behavior is controlled by the |
||
| 82 | <code>org.apache.tomcat.websocket.noAddAfterHandshake</code> servlet context |
||
| 83 | initialization parameter. The default may be changed by setting the |
||
| 84 | <code>org.apache.tomcat.websocket.STRICT_SPEC_COMPLIANCE</code> system |
||
| 85 | property to <code>true</code> but any explicit setting on the servlet context |
||
| 86 | will always take priority.</p> |
||
| 87 | |||
| 88 | <p>When using the WebSocket client to connect to server endpoints, the timeout |
||
| 89 | for IO operations while establishing the connection is controlled by the |
||
| 90 | <code>userProperties</code> of the provided |
||
| 91 | <code>javax.websocket.ClientEndpointConfig</code>. The property is |
||
| 92 | <code>org.apache.tomcat.websocket.IO_TIMEOUT_MS</code> and is the |
||
| 93 | timeout as a <code>String</code> in milliseconds. The default is 5000 (5 |
||
| 94 | seconds).</p> |
||
| 95 | |||
| 96 | <p>When using the WebSocket client to connect to secure server endpoints, the |
||
| 97 | client SSL configuration is controlled by the <code>userProperties</code> |
||
| 98 | of the provided <code>javax.websocket.ClientEndpointConfig</code>. The |
||
| 99 | following user properties are supported:</p> |
||
| 100 | <ul> |
||
| 101 | <li><code>org.apache.tomcat.websocket.SSL_CONTEXT</code></li> |
||
| 102 | <li><code>org.apache.tomcat.websocket.SSL_PROTOCOLS</code></li> |
||
| 103 | <li><code>org.apache.tomcat.websocket.SSL_TRUSTSTORE</code></li> |
||
| 104 | <li><code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code></li> |
||
| 105 | </ul> |
||
| 106 | <p>The default truststore password is <code>changeit</code>.</p> |
||
| 107 | |||
| 108 | <p>If the <code>org.apache.tomcat.websocket.SSL_CONTEXT</code> property is |
||
| 109 | set then the <code>org.apache.tomcat.websocket.SSL_TRUSTSTORE</code> and |
||
| 110 | <code>org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD</code> properties |
||
| 111 | will be ignored.</p> |
||
| 112 | |||
| 113 | <p>For secure server end points, host name verification is enabled by default. |
||
| 114 | To bypass this verification (not recommended), it is necessary to provide a |
||
| 115 | custom <code>SSLContext</code> via the |
||
| 116 | <code>org.apache.tomcat.websocket.SSL_CONTEXT</code> user property. The |
||
| 117 | custom <code>SSLContext</code> must be configured with a custom |
||
| 118 | <code>TrustManager</code> that extends |
||
| 119 | <code>javax.net.ssl.X509ExtendedTrustManager</code>. The desired verification |
||
| 120 | (or lack of verification) can then be controlled by appropriate |
||
| 121 | implementations of the individual abstract methods.</p> |
||
| 122 | |||
| 123 | <p>When using the WebSocket client to connect to server endpoints, the number of |
||
| 124 | HTTP redirects that the client will follow is controlled by the |
||
| 125 | <code>userProperties</code> of the provided |
||
| 126 | <code>javax.websocket.ClientEndpointConfig</code>. The property is |
||
| 127 | <ocde>org.apache.tomcat.websocket.MAX_REDIRECTIONS</ocde>. The default value |
||
| 128 | is 20. Redirection support can be disabled by configuring a value of zero. |
||
| 129 | </p> |
||
| 130 | |||
| 131 | <p>When using the WebSocket client to connect to a server endpoint that requires |
||
| 132 | BASIC or DIGEST authentication, the following user properties must be set: |
||
| 133 | </p> |
||
| 134 | <ul> |
||
| 135 | <li><code>org.apache.tomcat.websocket.WS_AUTHENTICATION_USER_NAME</code> |
||
| 136 | </li> |
||
| 137 | <li><code>org.apache.tomcat.websocket.WS_AUTHENTICATION_PASSWORD</code> |
||
| 138 | </li> |
||
| 139 | </ul> |
||
| 140 | <p>Optionally, the WebSocket client can be configured only to send |
||
| 141 | credentials if the server authentication challenge includes a specific realm |
||
| 142 | by defining that realm in the optional user property:</p> |
||
| 143 | <ul> |
||
| 144 | <li><code>org.apache.tomcat.websocket.WS_AUTHENTICATION_REALM</code></li> |
||
| 145 | </ul> |
||
| 146 | |||
| 147 | <p>When using the WebSocket client to connect to a server endpoint via a forward |
||
| 148 | proxy (also known as a gateway) that requires BASIC or DIGEST authentication, |
||
| 149 | the following user properties must be set: |
||
| 150 | </p> |
||
| 151 | <ul> |
||
| 152 | <li><code>org.apache.tomcat.websocket.WS_PROXY_AUTHENTICATION_USER_NAME |
||
| 153 | </code></li> |
||
| 154 | <li><code>org.apache.tomcat.websocket.WS_PROXY_AUTHENTICATION_PASSWORD |
||
| 155 | </code></li> |
||
| 156 | </ul> |
||
| 157 | <p>Optionally, the WebSocket client can be configured only to send |
||
| 158 | credentials if the server authentication challenge includes a specific realm |
||
| 159 | by defining that realm in the optional user property:</p> |
||
| 160 | <ul> |
||
| 161 | <li><code>org.apache.tomcat.websocket.WS_PROXY_AUTHENTICATION_REALM</code> |
||
| 162 | </li> |
||
| 163 | </ul> |
||
| 164 | |||
| 165 | </div></div></div></div></div><footer><div id="footer"> |
||
| 166 | Copyright © 1999-2025, The Apache Software Foundation |
||
| 167 | <br> |
||
| 168 | Apache Tomcat, Tomcat, Apache, the Apache Tomcat logo and the Apache logo |
||
| 169 | are either registered trademarks or trademarks of the Apache Software |
||
| 170 | Foundation. |
||
| 171 | </div></footer></div></body></html> |