To implement STOMP in Spring WebSocket, add spring-boot-starter-websocket, annotate a configuration class with @EnableWebSocketMessageBroker, register a STOMP endpoint, and configure a message broker with application and broker destination prefixes. Then write @MessageMapping methods that receive client messages and return values that Spring broadcasts to subscribers through @SendTo.
STOMP (Simple Text Oriented Messaging Protocol) is a small text protocol that runs on top of a WebSocket. Raw WebSockets only give you a pipe of frames; STOMP adds commands such as CONNECT, SUBSCRIBE and SEND, plus headers and destinations, so you get publish/subscribe messaging without inventing your own message format.
A STOMP client opens one WebSocket connection, sends a CONNECT frame, then subscribes to destinations such as /topic/messages. When it wants to publish, it sends a SEND frame to a destination such as /app/chat. Spring routes the two kinds of destinations differently:
| Destination prefix | Handled by | Typical use |
|---|---|---|
/app (application prefix) | @MessageMapping methods in your controllers | Messages the server must process |
/topic | Broker, delivered to every subscriber | Broadcast, publish/subscribe |
/queue | Broker, usually one consumer | Point-to-point style messages |
/user | SimpMessagingTemplate with user resolution | Messages to one authenticated user |
Spring ships with a simple in-memory broker, which is enough for one application instance. For several instances or durable delivery you replace it with an external STOMP broker such as RabbitMQ (see the broker relay section below).
With Maven, add the WebSocket starter. It brings in spring-websocket and spring-messaging.
For Gradle, use implementation 'org.springframework.boot:spring-boot-starter-websocket'. Spring Boot 3 and later use the jakarta.* namespace, so any code you copy from older tutorials with javax.* imports must be updated.
The configuration class implements WebSocketMessageBrokerConfigurer. It declares where clients connect and how destinations are routed.
addEndpoint("/ws") is the HTTP URL the browser upgrades to a WebSocket. setApplicationDestinationPrefixes("/app") sends every message whose destination starts with /app to your controllers. enableSimpleBroker turns on the in-memory broker for /topic and /queue.
Always list the exact allowed origins. Using setAllowedOriginPatterns("*") in production lets any website open a socket to your server using your users' cookies. Add .withSockJS() to the endpoint only if you must support very old browsers or proxies that block WebSockets; modern browsers do not need it.
A STOMP controller looks like a REST controller, but the method is bound to a destination instead of a URL. Use a record for the payload; Spring converts JSON with Jackson.
The client sends to /app/chat (the application prefix plus the mapping), and every client subscribed to /topic/messages receives the returned ChatEvent. Put each public record in its own file in a real project.
When a message should be triggered by a scheduled job, a REST call or another service, inject SimpMessagingTemplate and publish directly.
convertAndSendToUser delivers to /user/{username}/queue/notifications on the client side, which requires the connection to be authenticated so Spring knows the user name.
In the browser, use the maintained @stomp/stompjs package. The older stomp.js library is no longer maintained.
Use wss:// in production so the traffic is encrypted. The client reconnects automatically after reconnectDelay milliseconds, but you must subscribe again inside onConnect, because subscriptions do not survive a reconnect.
CONNECT frame, for example with a ChannelInterceptor that validates a token and sets the user. Spring Security can then restrict destinations with AbstractSecurityWebSocketMessageBrokerConfigurer (older) or the MessageMatcherDelegatingAuthorizationManager approach in current Spring Security.registry.enableStompBrokerRelay("/topic", "/queue") when you run several application instances, so a message sent through one instance reaches subscribers connected to another. The broker must support STOMP; RabbitMQ does with its STOMP plugin.WebSocketTransportRegistration to protect the server from slow or abusive clients.The /app prefix routes a message to your @MessageMapping methods, so your code processes it. The /topic prefix routes it to the broker, which delivers it to every subscriber without calling your controller.
No, not for current browsers. All maintained browsers support WebSocket, so withSockJS() is only a fallback for restrictive networks or legacy clients.
Yes. Use SimpMessagingTemplate.convertAndSendToUser or return a value with @SendToUser. The user must be authenticated so Spring can map the name to a session.
Only for a single instance and when losing messages on restart is acceptable. For clustering, durability or very high volumes, use a broker relay to RabbitMQ or another STOMP-capable broker.
Implementing STOMP in Spring WebSocket takes four pieces: the WebSocket starter, a @EnableWebSocketMessageBroker configuration with an endpoint and destination prefixes, @MessageMapping controllers (or SimpMessagingTemplate) on the server, and a STOMP client such as @stomp/stompjs in the browser. Restrict allowed origins, authenticate connections, and move to a broker relay when you scale beyond one instance.