When using websockets with Spring Boot I've seen examples that use:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic/");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/greeting");;
}
}
Specifying the config.setApplicationDestinationPrefixes("/app") and in the controller using the @MessageMapping annotation.
And I've also seen examples that use only the enableSimpleBroker() and in the controller use @SubscribeMapping.
For what I understood the @MessageMapping is responsible to route the received message to the correct method. And methods with this annotation will only be triggered if the destination contains one of the prefixes declared in setApplicationDestinationPrefixes.
But @SubscribeMapping also route the message to the correct method and we don't need to call setApplicationDestinationPrefixes() in the config class.
What's the difference?