How to Use STOMP with Spring Boot: Simple Guide
To use
STOMP with Spring Boot, configure WebSocket support with a @EnableWebSocketMessageBroker annotated class and define message broker settings. Then create controller methods with @MessageMapping to handle messages and use SimpMessagingTemplate to send messages to clients.Syntax
Using STOMP with Spring Boot involves three main parts:
- WebSocket Configuration: Enable WebSocket message broker and configure endpoints.
- Message Handling: Use
@MessageMappingto receive messages from clients. - Sending Messages: Use
SimpMessagingTemplateto send messages to subscribed clients.
java
import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/topic"); registry.setApplicationDestinationPrefixes("/app"); } }
Example
This example shows a Spring Boot app that uses STOMP over WebSocket to broadcast messages to clients subscribed to /topic/greetings. The client sends messages to /app/hello, which the server processes and broadcasts back.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Controller; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @SpringBootApplication public class StompSpringBootApplication { public static void main(String[] args) { SpringApplication.run(StompSpringBootApplication.class, args); } } @Controller class GreetingController { @MessageMapping("/hello") @SendTo("/topic/greetings") public String greeting(String message) throws Exception { Thread.sleep(1000); // simulated delay return "Hello, " + message + "!"; } } @Configuration @EnableWebSocketMessageBroker class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/topic"); registry.setApplicationDestinationPrefixes("/app"); } }
Output
Server starts and listens on /ws endpoint for WebSocket connections.
When a client sends a message to /app/hello with payload "World", server responds by broadcasting "Hello, World!" to /topic/greetings.
Common Pitfalls
- Not enabling
@EnableWebSocketMessageBrokercauses no WebSocket support. - Forgetting to set
setApplicationDestinationPrefixesleads to message routing failures. - Not registering STOMP endpoints with SockJS fallback can cause client connection errors.
- Using mismatched destination prefixes between client and server causes messages to be lost.
java
/* Wrong: Missing @EnableWebSocketMessageBroker */ @Configuration public class WrongConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws"); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/topic"); } } /* Right: Correct annotation and SockJS fallback */ @Configuration @EnableWebSocketMessageBroker public class CorrectConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/topic"); registry.setApplicationDestinationPrefixes("/app"); } }
Quick Reference
STOMP with Spring Boot Cheat Sheet:
@EnableWebSocketMessageBroker: Enable WebSocket message handling.registerStompEndpoints(): Define WebSocket endpoint with SockJS fallback.configureMessageBroker(): Setup broker and application prefixes.@MessageMapping: Map incoming messages to methods.@SendTo: Define destination to broadcast messages.SimpMessagingTemplate: Send messages programmatically.
Key Takeaways
Enable WebSocket message broker with @EnableWebSocketMessageBroker and configure endpoints.
Use @MessageMapping to handle client messages and @SendTo to broadcast responses.
Always set application destination prefixes and enable a simple broker for topics.
Register STOMP endpoints with SockJS fallback for better client compatibility.
Test message routing carefully to avoid mismatched destination prefixes.