spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.69k stars 38.14k forks source link

Improve STOMP WebSocket documentation for input message buffer size #31616

Closed monkeycode0 closed 12 months ago

monkeycode0 commented 1 year ago

I send a message to the server from a web UI.

When the data size is greater than 64K, the server does not respond to the WEB client.

How can I change the max buffer size?

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean factoryBean = new ServletServerContainerFactoryBean();
        factoryBean.setMaxBinaryMessageBufferSize(2*1024*1024);
        factoryBean.setMaxTextMessageBufferSize(2*1024*1024);
        factoryBean.setMaxSessionIdleTimeout(60 * 1000L);
        return factoryBean;
    }
}

I'd like to use this config, but it does not work.

sbrannen commented 1 year ago

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

monkeycode0 commented 1 year ago

I've edited your comment to improve the formatting. You might want to check out this Mastering Markdown guide for future reference.

Thank you very much!

rstoyanchev commented 12 months ago

Thanks for the report.

The above should adjust the input buffer size of a standard (JSR-356) WebSocket server. Separately, there is a size limit for incoming messages at the STOMP level that you can set through the configureWebSocketTransport callback of WebSocketMessageBrokerConfigurer. If you turn logging up, you'll look for indications whether the message is rejected at the WebSocket server or at the level of STOMP handling.

If this does not work, please share a small project.

monkeycode0 commented 12 months ago

Thanks for the report.

The above should adjust the input buffer size of a standard (JSR-356) WebSocket server. Separately, there is a size limit for incoming messages at the STOMP level that you can set through the configureWebSocketTransport callback of WebSocketMessageBrokerConfigurer. If you turn logging up, you'll look for indications whether the message is rejected at the WebSocket server or at the level of STOMP handling.

If this does not work, please share a small project.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic", "/direct");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/manager/{userId}", "/client/{userId}")
                .setAllowedOrigins("*")
                .setHandshakeHandler(new CustomHandshakeHandler())
        ;
    }

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        registry.setMessageSizeLimit(128 * 1024 * 1024);
        registry.setSendBufferSizeLimit(128 * 1024 * 1024);
        registry.setSendTimeLimit(60 * 1000);
        WebSocketMessageBrokerConfigurer.super.configureWebSocketTransport(registry);
    }

}

yes, you are right! i override this method configureWebSocketTransport ,id does work!

when i use the gateway, it also should configure this:

spring: 
  cloud:
    gateway: 
      httpclient:
        websocket:
          max-frame-payload-length: 5242880
rstoyanchev commented 12 months ago

Good that it's working. I think the STOMP WebSocket Server section of the documentation could be improved a little based on this. Also the Server Configuration under WebSockets could also mention that with STOMP there is additional transport config.