Quick revision
The short answer
@EnableWebSocketMessageBroker turns on STOMP messaging over WebSocket: Spring registers the endpoint clients connect to, the channels that carry messages, and a message broker, either a simple in-memory one or a relay to an external broker. Clients send to destinations with an application prefix, such as /app/chat, which are routed to @MessageMapping methods; those methods, or any bean using SimpMessagingTemplate.convertAndSend, send to broker destinations such as /topic/room1, which the broker delivers to every subscriber. convertAndSendToUser targets one user's destination based on the authenticated principal.
What to remember
In this answer, we saw that @EnableWebSocketMessageBroker sets up STOMP over WebSocket: an endpoint, inbound and outbound channels, routing by destination prefix, and a simple in-memory broker or a relay to an external one. Frames sent to the application prefix, such as /app/chat, reach @MessageMapping methods, whose results go to broker destinations through @SendTo, while SimpMessagingTemplate.convertAndSend lets any bean publish to destinations like /topic/room1. convertAndSendToUser delivers to one user's sessions, resolved from the authenticated principal.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@EnableWebSocketMessageBroker turns on STOMP messaging over WebSocket: Spring registers the endpoint clients connect to, the channels that carry messages, and a message broker, either a simple in-memory one or a relay to an external broker. Clients send to destinations with an application prefix, such as /app/chat, which are routed to @MessageMapping methods; those methods, or any bean using SimpMessagingTemplate.convertAndSend, send to broker destinations such as /topic/room1, which the broker delivers to every subscriber. convertAndSendToUser targets one user's destination based on the authenticated principal.
Why this question is asked
Raw WebSocket gives us a pipe of messages and nothing else: no addresses, no subscriptions, no way to say "this message is for room 1". STOMP adds those on top, and Spring's support makes a WebSocket application look much like a Spring MVC one, with annotated handler methods. Interviewers ask about these three pieces because they are the core of every Spring chat or live-update example, and confusing the application prefix with the broker prefix is the most common mistake. In this answer, we will recap STOMP, see what the annotation configures, follow a message through @MessageMapping and @SendTo, push from a service with SimpMessagingTemplate, and send to a single user.
1A Quick Recap of STOMP Over WebSocket
STOMP is a small text protocol for messaging. A client CONNECTs, then SUBSCRIBEs to destinations, which are just strings like /topic/room1, and SENDs messages to destinations. The server delivers MESSAGE frames to subscribers. Each frame has a command, headers such as destination and content-type, and a body, usually JSON. Over WebSocket, each STOMP frame travels as one WebSocket message.
This gives us publish-subscribe for free: the client says which destinations it cares about, and anything sent to those destinations reaches it. Spring's job is to decide which frames go to our code and which go straight to the broker.
2What the Annotation Configures
We put @EnableWebSocketMessageBroker on a configuration class that implements WebSocketMessageBrokerConfigurer, and fill in two methods.
- WebSocket endpoint /wshandshake, one session per client
- clientInboundChannelSTOMP frames from clients
- Router by prefix/app -> controllers, /topic and /queue -> broker
- @MessageMapping methodsour application code
- Broker (simple or relay)subscriptions and delivery
- clientOutboundChannelMESSAGE frames back to clients
6 layers, from the top down. Step through them one at a time.
Behind these few lines Spring builds a small messaging system. Frames from clients enter the clientInboundChannel. A router looks at the destination: if it starts with /app, the frame goes to the annotated controller methods; if it starts with /topic or /queue, it goes to the broker. The broker keeps track of subscriptions and sends MESSAGE frames to clients through the clientOutboundChannel. Messages our own code sends to the broker travel on the brokerChannel.
The simple broker is an in-memory map of subscriptions inside the application. It is fine for one instance. The broker relay instead forwards broker destinations over TCP to a real STOMP broker such as RabbitMQ or ActiveMQ, which is what we use when several instances must share subscriptions.
3@MessageMapping and @SendTo
A @MessageMapping method is the WebSocket version of a @PostMapping. Its destination is written without the application prefix, and the payload is converted from JSON just like a request body.
Notice the two different prefixes. The client sends to /app/chat/room1, which reaches our method, but subscribes to /topic/room1, which belongs to the broker. The method's return value is sent to the @SendTo destination; without @SendTo, Spring sends it to the same destination with the broker prefix (/topic/chat/room1 by default). This split is the reason our code can validate, enrich or reject messages before anyone sees them.
4Pushing From Anywhere With SimpMessagingTemplate
Many messages do not start with a client frame. A score changes, a job finishes, an order ships. For these, any Spring bean can inject SimpMessagingTemplate and send to a broker destination directly.
convertAndSend converts the object with the configured message converter, JSON by default, and puts it on the brokerChannel. So @SendTo and SimpMessagingTemplate work together: one sends replies to client messages, the other sends events produced elsewhere, and both end up at the same broker.
5Sending to One User With convertAndSendToUser
Topics reach everyone subscribed. For private messages we use user destinations. The client subscribes to /user/queue/notifications, and our code calls convertAndSendToUser with a user name.
5 messages in order. Play them, or step through them one at a time.
The user name comes from the Principal of the WebSocket session, which Spring takes from the authenticated HTTP handshake (for example with Spring Security) or which we set in a ChannelInterceptor when the CONNECT frame arrives with a token. A component called UserDestinationMessageHandler translates /user/alice/queue/notifications into the unique destinations of Alice's sessions, so another user cannot subscribe to her messages just by guessing a name. @SendToUser is the annotation counterpart for replying to the sender only.
6Follow-up questions
What is the difference between /topic and /queue?
For Spring's simple broker, nothing technical: both are just prefixes we enabled. By convention, /topic means publish-subscribe to many subscribers and /queue means messages for one recipient, which is why user destinations use /queue. With an external broker such as RabbitMQ the prefixes map to different broker concepts, so the convention starts to matter.
Why does my @MessageMapping method never get called?
Usually the client sends to the broker destination instead of the application destination, for example /topic/chat instead of /app/chat, so the frame goes straight to the broker and skips our code. Another cause is writing the mapping with the prefix included, @MessageMapping("/app/chat"), which then expects /app/app/chat.
How do you authenticate STOMP users?
Either authenticate the HTTP handshake, so the session's Principal is set from the HTTP security context, or send a token in the CONNECT frame's headers and validate it in a ChannelInterceptor registered on the clientInboundChannel, setting the user on the StompHeaderAccessor. Authorisation of SUBSCRIBE and SEND frames can then be added with Spring Security's message security rules.
7Conclusion
In this answer, we saw that @EnableWebSocketMessageBroker sets up STOMP over WebSocket: an endpoint, inbound and outbound channels, routing by destination prefix, and a simple in-memory broker or a relay to an external one. Frames sent to the application prefix, such as /app/chat, reach @MessageMapping methods, whose results go to broker destinations through @SendTo, while SimpMessagingTemplate.convertAndSend lets any bean publish to destinations like /topic/room1. convertAndSendToUser delivers to one user's sessions, resolved from the authenticated principal.
Open with
@EnableWebSocketMessageBroker enables STOMP messaging over WebSocket. In the configurer I register a STOMP endpoint like /ws, set an application destination prefix, usually /app, and enable a broker for prefixes like /topic and /queue, either the simple in-memory broker or enableStompBrokerRelay to RabbitMQ or ActiveMQ.
Then, if they want more
Underneath, Spring creates an inbound channel for client frames, routes them by prefix, and an outbound channel for messages to clients. When a client SENDs to /app/chat/room1, the frame is routed to a @MessageMapping("/chat/{room}") method in a controller, where I can validate it and get the Principal.
Whatever the method returns goes to the destination in @SendTo, say /topic/{room}, which is a broker destination, and the broker delivers it to everyone subscribed to /topic/room1. So clients send to the app prefix and subscribe to the broker prefix.
For messages that do not start from a client, any bean injects SimpMessagingTemplate and calls convertAndSend("/topic/room1", payload); it converts to JSON and puts it on the broker channel.
For private messages there is convertAndSendToUser("alice", "/queue/notifications", payload): the client subscribes to /user/queue/notifications, and Spring resolves the user name, which comes from the authenticated principal of the session, to that user's session-specific destinations on every tab.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does @EnableWebSocketMessageBroker configure, and how do @MessageMapping and...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/8a59d7abbe/