Spring boot + Websockets : Connection getting closed by itself.

Problem : Getting the message “Websocket already in CLOSED or CLOSING state” from browser console after sending the first message.

Reason: The packet size is larger than the one set for Text/Binary message on Spring WebSocket handler.

Fix: Increase size limit for individual packages/frames in the Spring handler –

[java]
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
session.setTextMessageSizeLimit(1024 * 1024);
session.setBinaryMessageSizeLimit(1024 * 1024);
log.info("Connection established");
}
[/java]

Kj
Kj [/.] Sreekumar programs computers as a hobby and profession. Into programming from his school days, Sree uses Codemarvels to key in facts and fixes he finds interesting while working on different projects. Some of the articles here give away a few shades of his philosophical leanings too.

Leave a Comment

Your email address will not be published. Required fields are marked *