Home » Java » java – Can't connect to websocket server (spring)-Exceptionshub

java – Can't connect to websocket server (spring)-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I try to implement plain websocket with Stomp (and not SockJs) with a Spring server and an Angular client.

Here is my code to enable websocket in Spring:

public class WebSocketConfiguration extends AbstractSecurityWebSocketMessageBrokerConfigurer {

    public TaskScheduler heartBeatScheduler() {
        return new ThreadPoolTaskScheduler();

    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker('/status').setHeartbeatValue(new long[] { 10000, 10000})


    public void registerStompEndpoints(StompEndpointRegistry registry) {

    protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {

    protected boolean sameOriginDisabled() {
        return true;

But I can’t connect to it, when I try in angular with stompjs the connection is never done.
When I test with wscat wscat -c "ws://localhost:4200/api/myapp-websocket" (the /api/ is good btw) there is no response, it’s always trying to connect without success.

What is wrong here ?

Edit: Here is the two dependency I use




I also try to change messages.anyMessage().authenticated(); to messages.anyMessage().permitAll(); but same behavior I still can’t connect to my websocket..

How to&Answers:

I suppose you send client requests from different port, that means your client is on different origin, for that you must add some headers on server side. Also on connect websocket sends POST to endpoint, which you must enable. I don’t know exactly what endpoints are being sent, check in you console, and then add them to antMatchers

public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    protected void configure(HttpSecurity http) throws Exception {

            .antMatchers("/status**", "/topic", "/myapp-websocket/**")


    public CorsConfigurationSource corsConfigurationSource() {
          CorsConfiguration config = new CorsConfiguration();
                    "GET", "POST", "PUT", "DELETE", "PATCH"));
          config.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
           UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", config);
            return source;