2

I have the following websocket configs + endpoints in my spring boot app:

Controller:

@Controller
public class QuoteController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @SubscribeMapping("/quote")
    public void singleQuote() {
        System.out.println("Salam");
    }
}

And the config:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    public static final String WEBSOCKET_HANDSHAKE_ENDPOINT_URI = "/api/wsocket";
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic"); /*Enable a simple in-memory broker for the clients to subscribe to channels and receive messages*/
        config.setApplicationDestinationPrefixes("/ws"); /*The prefix for the endpoints in the controller*/
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        /*websocket handshaking endpoint*/
        registry.addEndpoint(WEBSOCKET_HANDSHAKE_ENDPOINT_URI)
            /*TODO remove this after development*/
            .setAllowedOrigins("http://localhost:8080");
    }

}

And the web-based js client:

this.client = new Client();
this.client.configure({
  brokerURL: `ws://localhost:8553/api/wsocket`,
  onConnect: () => {
    console.log('onConnect');
    this.client.subscribe('/topic/quote', message => {
      console.log(message);
      debugger;
    });
  },
  // Helps during debugging, remove in production
  debug: (str) => {
    console.log(new Date(), str);
  }
});
this.client.activate();

I expect the singleQuote to be hit, but it doesn't. Why? Isn't the method annotated with SubscribeMapping is supposed to be called when a client subscribe to a channel ?

Arian
  • 7,397
  • 21
  • 89
  • 177
  • It just subscribes to the channel. you need to send something to the channel to hit singleQuote – Barath Nov 05 '18 at 07:30
  • Isn't that what MessageMapping do? – Arian Nov 05 '18 at 07:51
  • It is clearly explained here https://stackoverflow.com/questions/29085791/does-spring-subscribemapping-really-subscribe-the-client-to-some-topic – Barath Nov 05 '18 at 08:37

0 Answers0