In Spring Boot, you detect WebSocket disconnections on the server with SessionDisconnectEvent (STOMP) or afterConnectionClosed (raw handlers), and you detect dead connections with heartbeats. Reconnection is the client's job: the server cannot dial a browser back, so the client must retry with exponential backoff and then restore its subscriptions and any missed state.
A robust setup has three parts: server-side cleanup on disconnect, heartbeats and timeouts so half-open connections are noticed, and a client that reconnects automatically.
A WebSocket is a long-lived TCP connection, so it can end in several ways: the user closes the tab, a mobile device switches networks, a load balancer or proxy hits its idle timeout, or the server restarts during a deployment. The worst case is a half-open connection, where one side is gone but the other has not noticed. Neither side learns about it without periodic traffic, which is why heartbeats matter.
With STOMP over WebSocket, Spring publishes application events you can listen to with @EventListener:
SessionDisconnectEvent fires for clean closes and for connections that timed out, so use it to release resources: remove the user from presence lists, cancel scheduled pushes and free per-session state. The event may fire on a different thread than the one that handled messages, so keep the cleanup thread-safe.
For a plain (non-STOMP) endpoint, extend TextWebSocketHandler instead:
Heartbeats let both sides notice a dead peer. With the STOMP simple broker, set the heartbeat intervals and give the broker a TaskScheduler:
The two numbers are the heartbeat the server sends and the heartbeat it expects, in milliseconds. setSendTimeLimit and setSendBufferSizeLimit protect the server from slow clients that never read. Keep the heartbeat interval shorter than the idle timeout of any proxy or load balancer in front of the app, otherwise the proxy will close quiet connections.
The reliable pattern is exponential backoff with jitter, so thousands of clients do not reconnect in the same second after a server restart. For STOMP, the maintained @stomp/stompjs library does this for you:
onConnect runs again after every successful reconnect, so put your subscriptions there. If you use the browser WebSocket API directly, write the backoff yourself:
A new connection is a new session, so messages sent while the client was offline are lost unless you design for it. Common approaches:
enableStompBrokerRelay when you run several server instances, so messages survive a single node restart.Authenticate again on every reconnect. Tokens may have expired while the client was offline, so refresh them before calling activate() again.
No. The server cannot initiate a connection to a browser. The client must reconnect, and libraries such as @stomp/stompjs provide that with reconnectDelay.
Use heartbeats. Without them a half-open TCP connection can stay "open" on the server for a long time. With STOMP heartbeats, the broker closes the session when it stops receiving beats, and SessionDisconnectEvent fires.
Usually a reverse proxy or load balancer idle timeout. Send heartbeats more often than that timeout, or raise the timeout for the WebSocket route.
Modern browsers all support WebSocket, so SockJS is rarely needed. Add .withSockJS() only if you must support restrictive proxies that block upgrades.
Listen for SessionDisconnectEvent or afterConnectionClosed to clean up, enable heartbeats shorter than your proxy timeout, and let the client reconnect with backoff and jitter. After reconnecting, re-subscribe, re-authenticate and recover missed messages explicitly.