Video in progress
What do the /app and /topic prefixes mean in a Spring STOMP configuration?
/app is the application destination prefix: messages starting with it go to your @MessageMapping methods. /topic (and /queue) are broker prefixes: those messages go straight to the broker and are distributed to subscribers.
📖 Answer
@Configuration
@EnableWebSocketMessageBroker
public class StompConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOrigins("https://example.uz");
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic", "/queue");
}
}
The flow looks like this:
- the client does
SEND /app/chat, and@MessageMapping("/chat")in the controller runs; - the controller sends to
/topic/chat, and the broker distributes it to subscribers; - the client does
SUBSCRIBE /topic/chatand receives the messages.
A client can also SEND /topic/chat, and it works: the message bypasses the controller and goes straight to everyone. That is often a security hole, because anyone can write to the topic with no checks. So it's good practice to forbid clients from SENDing to /topic/** in Spring Security.
The names /topic and /queue are only a convention: for the simple broker there is no technical difference between them.
Sign up to mark studied questions and save your progress.