I followed a tutorial to implement websockets in my Java Spring application. It is working fine so far but I really would like to understand what this is used for:
config.setApplicationDestinationPrefixes("/app");
My whole config looks like this
@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("/hello").withSockJS();
}
}
I basically just don't understand the given explanations in spring docs / the tut - e.g.
... it designates the "/app" prefix for messages that are bound for @MessageMapping-annotated methods.
setApplicationDestinationPrefixes is used as prefix to message mapping while sending messages from client using STOMP. So if your client sending data using STOMP for @MessageMapping("add") then the stompClient.send("/app/add"..)
In other words, it has no real meaning other than assuring that all messages that will be received on the server and are having one of the prefixes from the list set by setApplicationDestinationPrefixes
will be interpreted by one of your methods that you annotated with @MessageMapping
annotation.
Simpler put - methods annotated by @MessageMapping
will be triggered only if the message has one of the prefixes in the list.
setApplicationDestinationPrefixes("/app") - used to
Configure one or more prefixes to filter destinations targeting application annotated methods. When messages are processed, the matching prefix is removed from the destination in order to form the lookup path. This means annotations should not contain the destination prefix.1
reference:- https://helptechcommunity.wordpress.com/2020/01/28/websocket-chat-application-using-spring-boot-and-react-js/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With