A Java WebSocket is a long-lived, full-duplex connection between a client and a server, created by upgrading an HTTP request and then exchanging messages in both directions over one TCP connection. In Java you build server endpoints with the Jakarta WebSocket API (jakarta.websocket, formerly javax.websocket from Java EE 7) and clients with either the same API or the java.net.http.WebSocket class in the JDK (Java 11 and later).
Unlike plain HTTP request and response, either side can send data at any time. That makes WebSockets a good fit for chat, live dashboards, notifications, collaborative editing and multiplayer features.
A WebSocket starts as a normal HTTP/1.1 request with an Upgrade: websocket header. If the server agrees, it answers 101 Switching Protocols, and from then on the same TCP connection carries WebSocket frames instead of HTTP messages. The URL schemes are ws:// (plain) and wss:// (TLS). Use wss:// in production so that proxies cannot tamper with the traffic and browsers do not block mixed content.
| Aspect | HTTP request/response | WebSocket |
|---|---|---|
| Direction | Client asks, server answers | Either side sends at any time |
| Connection | Short lived (or reused for requests) | One long-lived connection |
| Per-message overhead | Full headers each time | A few bytes of frame header |
| Typical use | Pages, REST APIs | Live updates, chat, games |
Messages are either text (UTF-8) or binary, and the protocol also defines control frames: ping, pong and close.
The Jakarta WebSocket API is part of Jakarta EE and is available in servlet containers such as Tomcat, Jetty, GlassFish, WildFly and Payara. Annotate a class with @ServerEndpoint and mark lifecycle methods with @OnOpen, @OnMessage, @OnClose and @OnError.
The container creates one endpoint instance per connection by default, so shared state such as the set of sessions must be static (or held in an injected bean) and thread safe. getAsyncRemote() sends without blocking the calling thread. getBasicRemote() blocks until the message is written and must not be called concurrently on the same session.
If you deploy to an embedded or non-Jakarta EE server, you may need to register endpoints programmatically (for example with ServerContainer.addEndpoint) or add a Tomcat or Jetty WebSocket dependency.
Browsers ship a built-in WebSocket object, so the client side needs no library:
If the context path of your web application is /app, the endpoint URL is wss://example.com/app/chat.
You do not need a third-party library to talk to a WebSocket server from Java. java.net.http.HttpClient can open a WebSocket and delivers events to a WebSocket.Listener.
Note the demand model: the listener receives a message only after you call request(n). The default onOpen requests one message for you, and you must request again after each one.
new WebSocket(...), so tokens usually travel in the query string or a cookie.Origin header to stop other sites from opening connections on behalf of a logged-in user (cross-site WebSocket hijacking).In Spring applications you can use spring-websocket (WebSocketHandler) or STOMP over WebSocket, and Spring WebFlux offers a reactive WebSocketHandler. These sit on top of the same protocol.
HTTP is request and response: the client always speaks first. A WebSocket keeps one connection open so that both sides can push messages at any time with very little overhead.
javax.websocket still used?Only in Java EE 8 and older (and Jakarta EE 8) servers. Since Jakarta EE 9 the package is jakarta.websocket, so moving to a current server means changing the imports.
Use Server-Sent Events when you only need server-to-client updates, such as a live feed. They work over plain HTTP and reconnect automatically. Choose WebSockets when the client also sends frequent messages.
Yes, if they support the HTTP upgrade and have idle timeouts longer than your heartbeat interval. Always serve over wss://, which also helps traverse restrictive proxies.
A Java WebSocket gives you a persistent, two-way channel after an HTTP upgrade. Use @ServerEndpoint from jakarta.websocket on the server, the browser WebSocket object or the JDK java.net.http.WebSocket on the client, and add authentication, origin checks, heartbeats and a shared broker before going to production.