Video in progress
Why was SockJS needed, and is it still mandatory to enable it today?
SockJS is a fallback layer that automatically switches to HTTP streaming or long polling where WebSocket doesn't work (old browsers, proxies that cut WebSocket). Today almost every browser and piece of infrastructure supports WebSocket, so in many projects it isn't required.
📖 Answer
Enabling it in Spring is a single call:
registry.addEndpoint("/ws").withSockJS();
The SockJS client first sends a request to /ws/info, then picks the best transport: WebSocket first, then xhr-streaming, and xhr-polling as the last resort.
An important nuance: you can't connect to a SockJS endpoint with a plain new WebSocket('ws://host/ws'). You need the SockJS client or a special path like /ws/websocket. If the frontend tries to connect directly with @stomp/stompjs while the server is registered only with withSockJS(), the handshake fails with 404 or 400.
In practice I start new projects without SockJS. If your clients include strict corporate networks and the logs show many handshake failures, you can add it later. You can also register one endpoint both ways.
Sign up to mark studied questions and save your progress.