In WebSocket programming, the Session object represents one open connection between a single client and the server. With the Jakarta WebSocket API it is jakarta.websocket.Session; it carries the session ID, connection state and user information, and it is the handle you use to send messages and close the connection.
It is not the HTTP session. A WebSocket session starts at the handshake and ends when the socket closes, and each browser tab that connects gets its own.
When a client completes the upgrade handshake, the container creates one Session and passes it to your @OnOpen method. The same instance is given to @OnMessage, @OnClose and @OnError, so you can identify which client acted. Typical uses are:
getId().isOpen().getUserProperties().getRequestParameterMap(), getPathParameters() and getUserPrincipal().| Method | Purpose |
|---|---|
getId() | Unique ID of the connection |
isOpen() | Whether the connection is open |
getBasicRemote() | Blocking sender |
getAsyncRemote() | Non-blocking sender returning a Future |
getUserProperties() | Mutable map for your own per-connection data |
getUserPrincipal() | Authenticated user from the handshake, or null |
getRequestParameterMap() | Query parameters of the handshake request |
getOpenSessions() | All open sessions of the same endpoint |
setMaxIdleTimeout(long) | Closes the connection after the idle period, in milliseconds |
close(CloseReason) | Closes the connection with a status code |
Expected output on the client after sending hi:
The exact session ID format depends on the container.
Spring has its own abstraction, org.springframework.web.socket.WebSocketSession, used with WebSocketHandler:
WebSocketSession.getAttributes() holds data copied from the HTTP session by a HandshakeInterceptor, which is how you carry a user ID into the socket. With STOMP messaging, the session ID is available in SimpMessageHeaderAccessor.getSessionId(), and Spring publishes SessionConnectEvent and SessionDisconnectEvent application events.
@OnClose and @OnError, otherwise closed connections leak memory.ConcurrentWebSocketSessionDecorator for this; with the Jakarta API use a lock or queue per session.isOpen() before sending, and handle IOException since the client may have disconnected.setMaxIdleTimeout to detect dead connections behind proxies and load balancers.No. An HTTP session spans many requests of one browser, while a WebSocket session is one connection. You can link them by copying attributes during the handshake.
Keep a map from your user ID to their Session (or use STOMP user destinations in Spring) and send to that session.
The container calls @OnClose and the session becomes closed, so isOpen() returns false. Remove it from your registry.
No. It is bound to a single server connection. Use a shared broker such as Redis or RabbitMQ to relay messages between nodes.
The WebSocket Session is the per-connection handle for identifying clients, sending messages, storing connection data and closing the socket. Track sessions in a thread-safe registry, clean them up on close and error, guard concurrent sends, and keep in mind that scaling beyond one server needs a message broker, since sessions do not move between nodes.