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]

Sreekumar Kj
Sreekumar (KJ) has been a hobby programmer from school days. Codemarvels is his personal blog from the year 2010, where he writes about technology, philosophy, society and a bit about physics. He now runs a conversational AI company - DheeYantra - focusing his efforts to help businesses improve operational efficiency using digital employees powered by AI.