Search code examples
spring-bootspring-securitywebsocketstompsockjs

Spring Boot and WebSockets - got connection lost when sending high throuput through secured websocket using Stomp protocol


I have 2 WebSockets endpoints:

registry.addEndpoint("/ws-handshake").withSockJS();
registry.addEndpoint("/api/admin/ws-handshake").withSockJS();

First one is without authentication, second one is secured with Spring Boot Security, uses same configuration as the HTTP security, and uses OAuth2. An authorization header is used to connect to the secure endpoint.

I use a simple for loop, as this is a POC but same amount of data will be used in production, to return a lot of records, this is done by using the SimpMessageSendingOperations class provided by Spring Boot:

    private SimpMessageSendingOperations messagingTemplate;

    @MessageMapping("/tail-topic")
    public void tailLogSendToTopic(@Payload WebSocketPoCPayload payload) throws InterruptedException {
        String topicName = "/topic/logentries";
        for (int i = 0; i < 3000; i++) {
            WebSocketPoCMessage message = new WebSocketPoCMessage(String.format("%d : returning payload %s", i, payload.getName()));
            messageTemplate.convertAndSend(topicName, message);
            // TimeUnit.MILLISECONDS.sleep(50);
        }
        log.info("done returning all messages");
    }

When I send a request via the unsecured endpoint everything runs fine, I receive everything clientside.

When I send a request via the secured endpoint everything runs fine until a certain payload number (for example 1499, is always the same payload number). Unless I uncomment the timeout, but needs to be more than 10 milliseconds than again I get a connection lost but on later number.

Seems like my security settings disconnected the connection if they see too much traffic, am I missing something in my security configuration?


Solution

  • As there seems to be no response on this I will accept the temporary workaround as an solution:

    private SimpMessageSendingOperations messagingTemplate;
    
    @MessageMapping("/tail-topic")
    public void tailLogSendToTopic(@Payload WebSocketPoCPayload payload) throws InterruptedException {
        String topicName = "/topic/logentries";
        for (int i = 0; i < 3000; i++) {
            WebSocketPoCMessage message = new WebSocketPoCMessage(String.format("%d : returning payload %s", i, payload.getName()));
            messageTemplate.convertAndSend(topicName, message);
            TimeUnit.MILLISECONDS.sleep(50);
        }
        log.info("done returning all messages");
    }