WebSocket is a new protocol for bidirectional communications initiated via HTTP/1.1 upgrade and providing basic message framing, layered over TCP. It is based on a low-level framing protocol that delivers messages in either UTF-8 TEXT or BINARY format.
A single message in WebSocket can be of any size (the underlying framing however does have a single frame limit of 63-bits). There can be an unlimited number of messages sent. Messages are sent sequentially, the base protocol does not support interleaved messages.
A WebSocket connection goes through some basic state changes:
State | Description |
---|---|
CONNECTING |
A HTTP Upgrade to WebSocket is in progress |
OPEN |
The HTTP Upgrade succeeded and the socket is now open and ready to read / write |
CLOSING |
A WebSocket Close Handshake has been started |
CLOSED |
WebSocket is now closed, no more read/write possible |
When a WebSocket is closed, a status code and short reason string is provided.
Jetty provides an implementation of the following standards and specs.
- RFC-6455
-
The WebSocket Protocol
We support the version 13 of the released and final spec.
Jetty tests its WebSocket protocol implementation using the autobahn testsuite.
ImportantThe early drafts of WebSocket were supported in Jetty 7 and Jetty 8, but this support has been removed in Jetty 9. This means that Jetty 9 will not support the old browsers that implemented the early drafts of WebSocket. (such as Safari 5.0 or Opera 12)
TipWant to know if the browser you are targeting supports WebSocket? Use caniuse.com/websockets to find out.
- JSR-356
-
The Java WebSocket API (
jakarta.websocket
)This is the official Java API for working with WebSockets.
Unstable standards and specs:
- perframe-compression
-
Per Frame Compression Extension.
An early extension draft from the Google/Chromium team that would provide WebSocket frame compression. perframe-compression using deflate algorithm is present on many versions of Chrome/Chromium.
Jetty’s support for perframe-compression is based on the draft-04 spec. This standard is being replaced with permessage-compression.
- permessage-compression
-
Per Frame Compression Extension.
This is the replacement for perframe-compression, switching the compression to being based on the entire message, not the individual frames.
APIs and libraries to implement your WebSockets using Jetty.
- Jetty WebSocket API
-
The basic common API for creating and working with WebSockets using Jetty.
- Jetty WebSocket Server API
-
Write WebSocket Server Endpoints for Jetty.
- Jetty WebSocket Client API
-
Connect to WebSocket servers with Jetty.
- Java WebSocket Client API
-
The new standard Java WebSocket Client API (
jakarta.websocket
) [JSR-356] - Java WebSocket Server API
-
The new standard Java WebSocket Server API (
jakarta.websocket.server
) [JSR-356]
To enable Websocket, you need to enable the websocket
module.
Once this module is enabled for your Jetty base, it will apply to all webapps deployed to that base. If you want to be more selective about which webapps use Websocket, then you can:
- Disable Websocket for a particular webapp
-
You can disable jsr-356 for a particular webapp by setting the context attribute
org.eclipse.jetty.websocket.jakarta
tofalse
. This will mean that websockets are not available to your webapp, however deployment time scanning for websocket-related classes such as endpoints will still occur. This can be a significant impost if your webapp contains a lot of classes and/or jar files. To completely disable websockets and avoid all setup costs associated with it for a particular webapp, use instead the context attributeorg.eclipse.jetty.containerInitializerExclusionPattern
, described next, which allows you to exclude the websocket ServletContainerInitializer that causes the scanning. - Completely disable Websocket for a particular webapp
-
Set the
org.eclipse.jetty.containerInitializerExclusionPattern
context attribute to includeorg.eclipse.jetty.websocket.jakarta.server.JakartaWebSocketServletContainerInitializer
. Here’s an example of doing this in code, although you can do the same in xml:WebAppContext context = new WebAppContext(); context.setAttribute("org.eclipse.jetty.containerInitializerExclusionPattern", "org.eclipse.jetty.websocket.jakarta.server.JakartaWebSocketServletContainerInitializer|com.acme.*");