Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is setApplicationDestinationPrefixes being used for?

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.

like image 628
Frnak Avatar asked Jul 12 '16 08:07

Frnak


3 Answers

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"..)

like image 106
Anil Verma Avatar answered Nov 08 '22 15:11

Anil Verma


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.

like image 35
Victor Avatar answered Nov 08 '22 16:11

Victor


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/

like image 1
Jolly Good Avatar answered Nov 08 '22 16:11

Jolly Good