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 ?