The @SendTo annotation tells Spring where to publish the return value of a @MessageMapping method. Without it, the result of a STOMP handler is broadcast to a default destination derived from the inbound one; with it, you choose the exact topic or queue, so every subscriber of that destination receives the message through the message broker.
It lives in org.springframework.messaging.handler.annotation and works with Spring's STOMP over WebSocket support. Its sibling @SendToUser targets a single user instead of a shared topic.
A STOMP client sends a frame to a destination such as /app/chat. Spring routes it to a method annotated with @MessageMapping("/chat"). The method return value is wrapped in a message and sent to the broker channel. @SendTo supplies the destination of that outgoing message.
The flow looks like this:
SEND frame to /app/chat.@MessageMapping method runs and returns an object.@SendTo destination, for example /topic/messages./topic/messages.@SendTo only works when a broker is enabled. In a Spring Boot 3 application (Jakarta namespace) the configuration is:
enableSimpleBroker turns on the in-memory broker for destinations starting with /topic and /queue. setApplicationDestinationPrefixes("/app") sends frames addressed to /app/... to your controller methods instead of the broker. For production setups that need clustering, replace the simple broker with enableStompBrokerRelay and an external broker such as RabbitMQ.
A client sends JSON to /app/chat, and every client subscribed to /topic/messages receives the returned ChatMessage. A browser client using the @stomp/stompjs library would look like this:
If a @MessageMapping method returns a value and has no @SendTo, Spring still sends the result. The destination is the inbound destination with the application prefix replaced by /topic. For /app/chat the default is /topic/chat. Adding @SendTo makes the routing explicit and independent of the mapping path, which is easier to maintain.
@SendTo supports placeholders that match @DestinationVariable values in the mapping:
A message sent to /app/rooms/42/chat is published to /topic/rooms/42.
Use @SendToUser when the reply is private, for example an error or a confirmation:
The client subscribes to /user/queue/reply, and Spring resolves it to a queue that belongs to the authenticated user's session. By default the reply goes to the user who sent the frame; set broadcast = false to deliver only to the sending session when the user has several.
When the destination is only known at runtime, or when you send from outside a message handler (a scheduled job, a REST controller, an event listener), inject SimpMessagingTemplate:
| Approach | Best for | Destination |
|---|---|---|
@SendTo | Replying to a handled message with a broadcast | Fixed or templated at compile time |
@SendToUser | Private reply to the sender | Per-user queue |
SimpMessagingTemplate | Sending from anywhere, computed destinations | Chosen at runtime |
@SendTo("/messages") does not start with a prefix the broker handles (/topic, /queue), nothing delivers it./app/... destination. That prefix routes back to your controllers, not to subscribers.void. With no return value there is nothing to send, so use SimpMessagingTemplate for explicit sends.@SendTo publishes to a destination that all subscribers share, such as /topic/messages. @SendToUser publishes to a per-user destination, so only that user's sessions receive the message.
Yes. The annotation accepts an array, for example @SendTo({"/topic/a", "/topic/b"}), and the return value is sent to each one.
No. It relies on Spring's messaging abstraction and a message broker. With a raw WebSocketHandler you send frames yourself through WebSocketSession.sendMessage.
Check that the client subscribes to the exact destination used in @SendTo, that the prefix is enabled in enableSimpleBroker, and that the message converter can serialise the returned type.
@SendTo routes the return value of a @MessageMapping method to a chosen broker destination, which turns a request handler into a broadcast. Enable a broker with @EnableWebSocketMessageBroker, keep application prefixes (/app) separate from broker prefixes (/topic, /queue), use @SendToUser for private replies, and fall back to SimpMessagingTemplate when you need to send from outside a handler or compute the destination at runtime.