Video in progress

What happens if you don't give a timeout when you create an SseEmitter?

new SseEmitter() gets the server's default async timeout, which on Tomcat is about 30 seconds. After that the connection is closed, AsyncRequestTimeoutException shows up in the logs, and the client reconnects every half minute.

📖 Answer

The symptom is familiar: everything works locally, but in DevTools you see the stream request being reopened every 30 seconds, and the logs have AsyncRequestTimeoutException or "response already committed" messages.

The fix is to set the timeout explicitly:

SseEmitter emitter = new SseEmitter(Duration.ofMinutes(30).toMillis());
emitter.onTimeout(emitter::complete);

Or globally:

spring:
  mvc:
    async:
      request-timeout: 30m

If you pass 0L, it waits forever. Be careful with that: if the client disappears and the server sends nothing, the emitter stays in memory forever. The server only finds out the connection is gone when the next send() throws an IOException.

So in practice I use a 15 to 30 minute timeout plus a small heartbeat comment every 20 to 30 seconds (emitter.send(SseEmitter.event().comment("ping"))). The heartbeat both keeps the proxy awake and finds dead clients quickly.