Differences from Spring
spring_boot_starter_websocket follows Spring's WebSocket API on the server (WebSocketHandler, WebSocketConfigurer, HandshakeInterceptor, the origin check) and Jakarta's names for annotated endpoints. Its clients have no counterpart in Spring. This page lists the differences.
Why
Spring's WebSocket client is imperative and does not reconnect; a Dart app on a mobile network needs a client that does. On the server, Dart has no servlet container, so the endpoints live in the web starter's router, and a few behaviours are safer by default.
Example
@WebSocketClient('chat', url: r'${chat.url}')
abstract interface class ChatSocket {
@OnMessage()
Stream<ChatMessage> messages();
}
WebSocketClient client = new StandardWebSocketClient();
client.execute(new TextWebSocketHandler() { ... }, "wss://chat.neon.books/ws");
Clients
| Spring | Here |
|---|---|
No declarative client; an imperative WebSocketClient or STOMP |
@WebSocketClient interfaces with typed streams, in the style of @FeignClient |
WebSocketConnectionManager does not reconnect |
It reconnects while someone listens (reconnect.*, named after Spring 7's RetryPolicy) |
Limits in code on the WebSocketContainer |
spring.websocket.client.max-text-message-size and max-binary-message-size; the text limit counts UTF-8 bytes (Tomcat counts characters) |
| No client properties | spring.websocket.client.config.* |
| No handshake interceptor bean | WebSocketHandshakeInterceptor runs at every (re)connection |
| Extensions such as permessage-deflate | None offered |
Server
| Spring | Here |
|---|---|
No annotated endpoint of its own; Jakarta's @ServerEndpoint through ServerEndpointExporter, one instance per connection |
@WebSocketEndpoint singleton beans with constructor injection, @PathVariable instead of @PathParam |
@EnableWebSocket |
Not needed |
| A handler method's error closes the connection with 1011 | It goes to handleTransportError / @OnError, and the connection stays open (without @OnError, it is closed with 1011) |
setAllowedOrigins, setAllowedOriginPatterns, setSupportedProtocols replace |
They add to the values set before |
HandshakeInterceptors on any registration |
On the handlers of a configurer only, not on @WebSocketEndpoints (a web filter covers both) |
An Origin that is not a URL: a 500 |
A 403; an Origin over 1024 characters is a 403 too |
| Host of the server's own origin compared with case | Without case |
| No registry of sessions | WebSocketSessionRegistry with broadcast |
| Tests on a real server | MockMvc.webSocket in memory, and MockWebSocketTransport for clients |
Not supported yet
- STOMP and SockJS.
- Extensions (permessage-deflate).
HandshakeInterceptors on@WebSocketEndpoints.