Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Spring WebSocket closes STOMP sessions on its own: send limits, first-message timeout and idle drops

Your error

Terminating 'StandardWebSocketSession[id=..., uri=ws://localhost:8080/ws]': Send time 10234 (ms) for session '...' exceeded the allowed limit 100003 more lines it can show

Fix it fast

Most common cause, 1 of 3

A slow client hits the send time or send buffer limit

Raise the limits in the STOMP transport registration if the clients are simply slow, and send less per client if the volume is the problem: fewer, smaller messages per topic.

Fix
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        registry.setSendTimeLimit(30 * 1000);
        registry.setSendBufferSizeLimit(1024 * 1024);
    }
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Spring Framework 7.1 closes a STOMP WebSocket session itself in two cases: sending to a slow client takes over 10 seconds or buffers over 512 KB ("exceeded the allowed limit", close code 4500), or no STOMP frame arrives within 60 seconds of connecting. Silent drops after idle time mean no heartbeats. Tune configureWebSocketTransport, or enable broker heartbeats with a TaskScheduler.

  • Applies to Spring Framework 7.1, Spring Boot 4.2, Tomcat (embedded) and Jetty
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Search the application log for the three lines Spring writes when it closes a session itself. A match on the first two is cause 1, a match on the third is cause 2, and no match at all, while clients still drop after a quiet spell, is cause 3.

Run this first
grep -E "exceeded the allowed limit|exceeds the allowed limit|No messages received after" logs/app.log | tail -n 20

Then find the line you see in this list and open its fix:

  1. 1A slow client hits the send time or send buffer limitWARN Terminating '...': Send time ... exceeded the allowed limit 10000, or Buffer size ... exceeds the allowed limit 524288; the client sees close code 4500
  2. 2The client opens the socket but sends no STOMP frame within 60 secondsINFO No messages received after 60000 ms. Closing StandardWebSocketSession[...]
  3. 3Quiet connections are dropped because the simple broker sends no heartbeatsSpring logs none of the lines above, yet clients lose the connection after the same idle time, often behind a proxy or load balancer

What does the error look like?

  • Terminating 'StandardWebSocketSession[id=..., uri=ws://localhost:8080/ws]': Send time 10234 (ms) for session '...' exceeded the allowed limit 10000
  • Buffer size 524800 bytes for session '...' exceeds the allowed limit 524288
  • No messages received after 61052 ms. Closing StandardWebSocketSession[id=..., uri=ws://localhost:8080/ws].
  • CloseStatus[code=4500, reason=Failed to send message within the configured send limit]

Cause 1 of 3

A slow client hits the send time or send buffer limit

You seeWARN Terminating '...': Send time ... exceeded the allowed limit 10000, or Buffer size ... exceeds the allowed limit 524288; the client sees close code 4500

Check

Spring closes a STOMP WebSocket session with code 4500 when messages to that client back up past the send time limit (10 seconds by default) or the send buffer limit (512 KB by default). Messages to one session go out one at a time, so a client on a weak network, or a burst of broadcasts, fills the buffer while an earlier send is still in progress. The time limit is only checked when another message is queued.

Check
grep -c "exceeded the allowed limit\|exceeds the allowed limit" logs/app.log

Fix

Raise the limits in the STOMP transport registration if the clients are simply slow, and send less per client if the volume is the problem: fewer, smaller messages per topic.

Fix
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        registry.setSendTimeLimit(30 * 1000);
        registry.setSendBufferSizeLimit(1024 * 1024);
    }
}

Verify

Repeat the burst that used to cut clients off. The Terminating lines stop and clients keep their session.

Verify
grep -c "Terminating '" logs/app.log

Confirmed by docs.spring.io

Did this fix it?

Cause 2 of 3

The client opens the socket but sends no STOMP frame within 60 seconds

You seeINFO No messages received after 60000 ms. Closing StandardWebSocketSession[...]

Check

Spring closes a STOMP WebSocket that has carried no sub-protocol message for 60 seconds after the handshake, because it assumes the connection is broken. A plain WebSocket test tool that never sends CONNECT, or a client that waits for a token or a user action before it sends CONNECT, trips it.

Check
grep -n "No messages received after" logs/app.log | tail -n 5

Fix

Send the STOMP CONNECT frame as soon as the socket opens, with the token already in its headers. Only if a client really needs longer before its first frame, raise the window in the transport registration.

Fix
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
    registry.setTimeToFirstMessage(120 * 1000);
}

Verify

Open a client and wait past the old 60 second mark. The session stays open and no No messages received line appears.

Verify
grep -c "No messages received after" logs/app.log

Confirmed by docs.spring.io

Did this fix it?

Cause 3 of 3

Quiet connections are dropped because the simple broker sends no heartbeats

You seeSpring logs none of the lines above, yet clients lose the connection after the same idle time, often behind a proxy or load balancer

Check

A Spring STOMP connection that drops after a quiet period with nothing in the Spring log usually lacks heartbeats: the simple broker supports STOMP heartbeats only when it has a TaskScheduler. Without them nothing crosses the wire between messages, and a proxy or the WebSocket container's own idle timeout ends the connection.

Check
grep -rn "setHeartbeatValue\|setTaskScheduler\|setMaxSessionIdleTimeout\|setIdleTimeout" src/main/java/

Fix

Give the simple broker a TaskScheduler and a heartbeat value, and make the client offer heartbeats too: STOMP negotiates them per direction from the heart-beat headers of CONNECT and CONNECTED, and a CONNECT without that header counts as 0,0, which turns them off. If you set an idle timeout on the container, through ServletServerContainerFactoryBean or Jetty's setIdleTimeout, keep it longer than the heartbeat interval.

Fix
private TaskScheduler messageBrokerTaskScheduler;

@Autowired
public void setMessageBrokerTaskScheduler(@Lazy TaskScheduler taskScheduler) {
    this.messageBrokerTaskScheduler = taskScheduler;
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/queue/", "/topic/")
            .setHeartbeatValue(new long[] {10000, 20000})
            .setTaskScheduler(this.messageBrokerTaskScheduler);
}

Verify

In the browser's Network tab, open the WebSocket and watch its messages: heartbeat frames now pass while the page is idle, and the connection survives several minutes of silence.

Verify
grep -rn "setHeartbeatValue" src/main/java/

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Keep the transport limits in one WebSocketMessageBrokerConfigurer and review them whenever a topic starts sending larger payloads.
  • Turn on broker heartbeats from the first release, and keep every proxy timeout longer than the heartbeat interval.
  • Alert on the Terminating log line: each one is a client whose queued messages were dropped with the session.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.