@EnableWebSocketMessageBroker enables WebSocket messaging backed by a message broker and the STOMP sub-protocol. It lets you write controllers with @MessageMapping that receive messages from clients and publish results to subscribers on destinations such as /topic/chat, much like HTTP controllers handle requests.
It is the right choice for chat, live dashboards and notifications, where many clients subscribe to topics. For a plain, protocol-free channel, @EnableWebSocket is the simpler alternative.
The annotation imports DelegatingWebSocketMessageBrokerConfiguration. It creates the messaging infrastructure: channels for inbound and outbound messages, a SimpMessagingTemplate for sending from anywhere in your code, and the STOMP endpoint handler mappings. You customise it by implementing WebSocketMessageBrokerConfigurer (the older AbstractWebSocketMessageBrokerConfigurer class is deprecated and was removed in current Spring versions).
Two kinds of destinations exist:
| Prefix (your choice) | Purpose |
|---|---|
Application prefix, for example /app | Messages from clients routed to @MessageMapping methods |
Broker prefix, for example /topic, /queue | Destinations clients subscribe to; the broker forwards messages to them |
enableSimpleBroker starts an in-memory broker that lives inside your application. addEndpoint("/ws") is the URL clients use for the initial WebSocket handshake. Append .withSockJS() only if you need fallback transports.
The payload is converted from JSON by Spring's message converters. Any client subscribed to /topic/chat receives the returned object.
To send from elsewhere, for instance a scheduled job or a REST endpoint, inject SimpMessagingTemplate:
Browsers do not speak STOMP natively. A client library such as @stomp/stompjs handles the protocol:
The simple broker keeps subscriptions in one JVM, so it only works for a single instance and supports a subset of STOMP. For several instances, use a full broker that speaks STOMP, such as RabbitMQ with its STOMP plugin, through a relay:
Spring then forwards messages to the broker, which fans them out to every instance's clients.
Authenticate during the HTTP handshake with Spring Security so the STOMP session has a Principal. For per-destination rules, Spring Security provides message authorization, enabled with @EnableWebSocketSecurity in current releases. Always restrict allowed origins, and validate message payloads and sizes, because every subscriber receives what you broadcast.
/app messages are routed to your @MessageMapping methods. /topic and /queue destinations are handled by the broker and delivered to subscribers.
Only for a single node and modest load. For clustering, durability and full STOMP features, relay to an external broker.
Use convertAndSendToUser with a /queue destination. Spring resolves the user's sessions through the user destination prefix.
Yes. Every current browser supports WebSocket, so SockJS is only a fallback for restricted networks.
@EnableWebSocketMessageBroker brings STOMP messaging to Spring: configure prefixes and an endpoint in a WebSocketMessageBrokerConfigurer, write @MessageMapping controllers, and publish with SimpMessagingTemplate. Start with the simple broker, move to a broker relay when you scale out, and secure both the handshake and the destinations.