Video in progress
Which limits do you configure in Spring for WebSocket message size and send buffers?
Three main ones: the maximum size of an incoming message (setMessageSizeLimit), the send buffer for one session (setSendBufferSizeLimit), and the maximum time one send may take (setSendTimeLimit). The last two protect against slow clients.
📖 Answer
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
registry.setMessageSizeLimit(128 * 1024) // incoming STOMP message, default 64KB
.setSendBufferSizeLimit(1024 * 1024) // session buffer, default 512KB
.setSendTimeLimit(15_000); // ms, default 10s
}
What each one solves:
messageSizeLimit: if a client sends a message larger than this, the session is closed. Frontend developers who send a file or an image base64-encoded over WebSocket hit this quickly. Think before raising the limit: it's better to send large files through a regular HTTP upload.
sendBufferSizeLimit and sendTimeLimit: the server writes to the client, and the client reads slowly (a bad mobile network). Messages pile up in the session buffer. If the buffer goes over the limit, or a single send takes longer than the time limit, Spring closes the session. Without them, one slow client can eat memory and tie up the clientOutboundChannel threads.
There are also limits at the servlet container level (for example Tomcat's text and binary buffer sizes), configured through ServletServerContainerFactoryBean. If they end up smaller than the STOMP limit, the connection is closed with 1009 before it ever reaches Spring.
Sign up to mark studied questions and save your progress.