In Spring, SimpMessagingTemplate sends messages from the server to WebSocket clients. Its methods build a STOMP message, convert your payload to the wire format, and hand it to the broker for delivery to a destination or a specific user. The core methods are convertAndSend for destinations, convertAndSendToUser for individual users, and send for fully built messages.
This article focuses on the sending API: which method to pick, how headers work, and how to target one session.
All of these come from SimpMessageSendingOperations, which SimpMessagingTemplate implements.
| Method | Purpose |
|---|---|
convertAndSend(destination, payload) | Convert the payload and publish to a destination |
convertAndSend(destination, payload, headers) | Same, with extra message headers |
convertAndSend(destination, payload, postProcessor) | Modify the message before sending |
convertAndSendToUser(user, destination, payload) | Deliver to one user's sessions |
send(destination, message) | Send a Message you built yourself |
convertAndSend is what you use most of the time. send is for cases where you already hold a Message and need full control of the headers.
A client that subscribed to /topic/chat receives the object as JSON. Conversion uses the message converters configured for the WebSocket broker, which include Jackson when it is on the classpath.
The template builds the destination /user/{username}/queue/alerts internally, and Spring's user destination resolver maps it to every active session of that user. The client subscribes to /user/queue/alerts. The username must match Principal.getName() of the authenticated WebSocket connection.
You can change the /user prefix with registry.setUserDestinationPrefix(...) in the broker configuration.
Use the header overload to attach metadata, for example a custom header that the client reads from the STOMP frame.
Headers are plain string-keyed entries. Keep them small, because they travel with every message.
If a user has several tabs open and you want only one of them to receive a message, address the session ID. This needs a SimpMessageHeaderAccessor.
The client subscribes to /user/queue/private, and Spring routes the message to the session whose ID was used. The setLeaveMutable(true) call is required, otherwise the headers are frozen and the broker cannot add its own.
You can call the template from event listeners and REST controllers, not only from @MessageMapping methods.
Posting to the endpoint pushes the text to every subscriber, which is a common way to bridge a normal HTTP call into real-time updates.
Only those enabled on the broker, for example /topic and /queue from enableSimpleBroker. Destinations under the application prefix such as /app are for incoming messages routed to controllers.
The simple broker does not store messages. If nobody is subscribed, the message is dropped. Persist the data yourself and send it when the client subscribes.
The user name is probably not the Principal name, or the client subscribed to /queue/alerts instead of /user/queue/alerts.
Yes. Configure message converters in configureMessageConverters of your WebSocketMessageBrokerConfigurer.
Use convertAndSend to publish to a topic, convertAndSendToUser to reach one user, and a SimpMessageHeaderAccessor when you need to target a single session or add control headers. The template is a shared, thread-safe bean, so you can call it from services, controllers and scheduled tasks.