@SendTo tells Spring where to publish the return value of a STOMP message handler. When a client sends a message to a method annotated with @MessageMapping, the returned object is converted and sent to the destination named in @SendTo, where every client subscribed to it receives it. Without @SendTo, Spring uses a default destination derived from the incoming one.
It is the simplest way to broadcast in a Spring WebSocket application that uses STOMP over WebSocket with a message broker.
@SendTo only works with the annotated message-handling model, which you enable with @EnableWebSocketMessageBroker:
Messages whose destination starts with /app go to @MessageMapping methods. Destinations starting with /topic or /queue go to the broker, which forwards them to subscribers. Use enableStompBrokerRelay instead of the simple in-memory broker when you need RabbitMQ or ActiveMQ.
A browser client using @stomp/stompjs interacts like this:
The client publishes to /app/hello, the greet method runs, and the result is broadcast to everyone subscribed to /topic/greetings, including the sender.
If a handler returns a value and has no @SendTo, Spring sends it to the incoming destination with the application prefix replaced by /topic. A message to /app/hello would be answered on /topic/hello. Declaring @SendTo explicitly is clearer and avoids surprises when you rename mappings.
A void method sends nothing. You can also pass several destinations: @SendTo({"/topic/a", "/topic/b"}).
Destination templates in @SendTo are resolved from the variables in the @MessageMapping pattern, not from fields of the message body. Bind them with @DestinationVariable:
A message to /app/rooms/42/send is published to /topic/rooms/42. To route by a property of the payload, you cannot use @SendTo. Inject SimpMessagingTemplate and compute the destination in code.
@SendTo targets a shared destination. For a reply to only the user who sent the message, use @SendToUser:
The client subscribes to /user/queue/replies, and Spring routes the message to that user's session or sessions. By default it goes to all sessions of the user. Set broadcast = false to reply only to the originating session. This requires an authenticated Principal on the WebSocket session.
To send from outside a message handler, for example from a scheduled task or REST endpoint, inject SimpMessagingTemplate:
| Need | Use |
|---|---|
| Reply to all subscribers of a topic | @SendTo |
| Reply to the sender only | @SendToUser |
| Send from any bean, or compute destination from data | SimpMessagingTemplate |
Not by itself, because it publishes to a shared destination. Use @SendToUser or SimpMessagingTemplate.convertAndSendToUser.
Yes. A message converter, normally Jackson for JSON, converts the return value into the message payload.
Check that the client subscribed to exactly the destination in @SendTo, that the prefix is enabled in enableSimpleBroker, and that the client published to a destination under the application prefix such as /app/hello.
No. It belongs to STOMP messaging, not HTTP, and the response goes through the message broker to subscribers.
@SendTo routes the return value of a @MessageMapping method to a broker destination, so all subscribers receive it. Use template variables bound with @DestinationVariable for per-room destinations, @SendToUser for private replies, and SimpMessagingTemplate when you need to send from other code or decide the destination at runtime.