Search code examples
angularwebsocketstompspring-websocket

Stomp not receiving CONNECTED frame (Angular + Spring)


I'm currently implementing a backend to frontend message mechanism using SockJS and Stomp on my Spring Boot 2 + Angular 14 app

I get a random behaviour, sometimes everythings works, but sometimes the websocket doesn't receive any messages.

When it doesn't work, WS messages looks like this

no connected frame

But when it works

connected and subscribe frame

The main difference is that when it workds, it receive a CONNECTED frame and fire the subscription. I don't get why sometimes it doesn't receive the CONNECTED frame.

My backend configuration

@Configuration
@EnableWebSocketMessageBroker
class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry
            .addEndpoint("/socket/no")
            .setAllowedOriginPatterns("*").withSockJS();
    }
}

The frontend client (app.component.ts > ngOnInit)

    const socketUrl = `${window.origin}/socket/no`;
    const stompClient = new Client({
      brokerURL: socketUrl,
      connectHeaders: {},
      debug: function (str) {
        console.debug(str);
      },
      reconnectDelay: 5000,
      webSocketFactory: function () {
        return new SockJS(socketUrl);
      },
      onConnect: function () {
        stompClient.subscribe("/topic/no", function (msg) {
          console.log("message received");
        });
      },
    });

    stompClient.activate();
  

proxy.conf.json

"/socket/**": {
        "target": "http://localhost:8080",
        "secure": false,
        "ws": true
    },

I feel like it's more likely to work when i open the angular app on a small page. For example my homepage has a lot of component and take some time to load. But when i open the app with the url of a simple grid view it has more chance to work but not all times.

Due to this feeling, i think it's a frontend issue. Maybe a required angular component for websocket not loader or something like this.

On the backend side i see this log after the connect frame sent

ms.w.s.h.LoggingWebSocketHandlerDecorator New WebSocketServerSockJsSession[id=naeygmcp]

Solution

  • Finally, migrating from Spring Boot 2.4 to 2.7 solved the issue. Now my app is migrated to Spring Boot 3.0 since some month and the issue didn't came back

    Looks related to this https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-22971