@EnableWebSocket turns on Spring's low-level WebSocket support. Placed on a @Configuration class that implements WebSocketConfigurer, it lets you map URL paths to WebSocketHandler implementations, so your application can exchange messages with browsers over a persistent, full-duplex connection.
Use it when you want raw control over messages. If you want a publish-subscribe model with destinations and routing, use STOMP with @EnableWebSocketMessageBroker instead. This article covers the setup, a working echo handler, origin security and the choice between the two approaches.
The annotation imports DelegatingWebSocketConfiguration, which collects every WebSocketConfigurer bean and registers the handler mappings with the servlet container (Tomcat, Jetty or Undertow). Your configurer calls registerWebSocketHandlers to declare which handler serves which path. The handshake, upgrade from HTTP and session management are handled by Spring and the container's Jakarta WebSocket implementation.
In Spring Boot, add spring-boot-starter-websocket. Boot does not enable WebSocket handlers by itself, so the annotation is still required for the handler-based approach.
WebSocketHandler. Extending TextWebSocketHandler is the simplest way to handle text frames.WebSocketConfigurer on a configuration class and register the handler.@EnableWebSocket.A browser connects with the standard API:
Use wss:// in production so traffic is encrypted.
By default Spring only accepts same-origin handshakes. Use setAllowedOrigins with an explicit list. Avoid "*" for endpoints that use cookie-based sessions, because it permits any website to open a connection with the user's credentials.
The handshake is an ordinary HTTP request, so Spring Security protects the path the same way it protects other URLs. Authenticate the handshake, then read the user from session.getPrincipal() inside the handler. Validate every incoming message as untrusted input, and set limits on message size and idle time.
For environments where WebSocket is blocked, append .withSockJS() to the registration. Spring then serves SockJS transport fallbacks such as HTTP streaming and long polling on the same path. Most modern browsers support WebSocket natively, so enable the fallback only when you know you need it.
| Need | Choose |
|---|---|
| Custom binary or text protocol, full control | @EnableWebSocket with a WebSocketHandler |
Topics, user queues, @MessageMapping controllers | @EnableWebSocketMessageBroker with STOMP |
| Broadcast to many subscribers through a broker | STOMP with a broker relay |
The two can coexist in one application, but a single path should use only one of them.
afterConnectionClosed.ConcurrentWebSocketSessionDecorator or serialise sends.@EnableWebSocket exposes raw handlers that you code against. @EnableWebSocketMessageBroker adds STOMP messaging, destinations and an optional external broker on top of WebSocket.
Yes, for handler-based endpoints. The starter adds the dependencies, but you still declare the configuration yourself.
Most often the origin is not in the allowed list, or Spring Security rejected the handshake. Check setAllowedOrigins and the security rules for the path.
Yes. Extend BinaryWebSocketHandler or override handleBinaryMessage and use BinaryMessage.
@EnableWebSocket registers the infrastructure that maps paths to WebSocketHandler beans through a WebSocketConfigurer. Restrict allowed origins, authenticate the handshake, manage sessions carefully, and pick STOMP when you need destinations and broadcast rather than a raw channel.