The spring-boot-starter-websocket dependency adds Spring's WebSocket and messaging support to a Spring Boot web application. It brings in spring-websocket, spring-messaging and the servlet web starter, so you can build full-duplex, real-time endpoints either with raw WebSocket handlers or with STOMP messaging over WebSocket.
Boot supplies the embedded server and auto-configuration around it. You supply the handlers or message mappings.
The starter depends on spring-boot-starter-web (an embedded Tomcat by default, or Jetty if you swap it), spring-websocket for the WebSocket API and spring-messaging for the message abstractions and STOMP support.
This starter is for servlet (Spring MVC) applications. Reactive applications built with Spring WebFlux get WebSocket support from spring-boot-starter-webflux instead.
Use a plain handler when you control both ends of the protocol and want a simple text or binary channel.
Always restrict allowed origins in production. The default allows only same-origin requests, and a wildcard removes that protection.
STOMP is a simple text protocol with destinations and subscriptions. Spring maps it onto familiar controller-style code.
A client connects to /ws, sends to /app/chat and subscribes to /topic/messages. The in-memory simple broker is fine for one node. For several instances, use enableStompBrokerRelay to forward to an external broker such as RabbitMQ so messages reach every subscriber.
WebSocket handshakes start as an HTTP request, so Spring Security rules apply to the handshake URL. Authenticate during the handshake, for example with a session cookie or a token passed in a query parameter or first STOMP frame, and authorize messages with Spring Security's messaging support. Browsers do not let JavaScript set custom headers on the handshake, which is why tokens are often sent in the STOMP CONNECT frame instead.
| Aspect | Raw handler | STOMP |
|---|---|---|
| Protocol | You define it | Standard frames and destinations |
| Routing | Manual | @MessageMapping, topics, queues |
| Broadcasting | Manual session tracking | Built-in broker |
| Best for | Simple or custom binary protocols | Chat, notifications, dashboards |
No. The browser connects with the native WebSocket API or a STOMP library. Java clients can use StandardWebSocketClient or WebSocketStompClient.
Usually the origin check or Spring Security is rejecting the handshake. Check setAllowedOriginPatterns and your security rules for the endpoint.
Yes, with a broker relay to RabbitMQ or ActiveMQ, or by using sticky sessions with a single-node broker.
When data flows only from server to client. SSE is simpler and works over plain HTTP.
spring-boot-starter-websocket provides the libraries and embedded server needed for real-time communication in a servlet-based Spring Boot application. Use a WebSocketHandler for custom protocols and STOMP with @MessageMapping for topic-based messaging, restrict origins, secure the handshake, and add a broker relay when you run more than one instance.