Java WebSocket Libraries: Complete Guide to Developing Real-Time Applications
WebSocket is a protocol that enables two-way communication between a client and a server over a single long-lived TCP connection. In Java, there are several mature libraries that simplify working with WebSocket. In this article, we will review the main libraries, their capabilities, and provide practical code examples.
1. Java WebSocket API (JSR 356)
The standard API for working with WebSocket in Java EE / Jakarta EE. It is part of application servers (WildFly, TomEE, GlassFish) and allows creating both server and client endpoints.
Key features:
- Annotations
@ServerEndpointand@ClientEndpoint - Support for text and binary messages
- Session and lifecycle management
- Integration with CDI (Contexts and Dependency Injection)
Server endpoint example:
import javax.websocket.*;import javax.websocket.server.ServerEndpoint;import java.io.IOException;
@ServerEndpoint("/chat")public class ChatEndpoint {
@OnOpen public void onOpen(Session session) { System.out.println("New connection: " + session.getId()); session.getUserProperties().put("username", "Guest"); }
@OnMessage public String onMessage(String message, Session session) { String username = (String) session.getUserProperties().get("username"); return username + ": " + message; }
@OnClose public void onClose(Session session, CloseReason reason) { System.out.println("Connection closed: " + reason.getReasonPhrase()); }
@OnError public void onError(Session session, Throwable error) { error.printStackTrace(); }}Client example:
import javax.websocket.*;import java.net.URI;
@ClientEndpointpublic class ChatClient {
private Session session;
@OnOpen public void onOpen(Session session) { System.out.println("Connected to server"); this.session = session; }
@OnMessage public void onMessage(String message) { System.out.println("Received: " + message); }
public void sendMessage(String message) throws IOException { if (session != null && session.isOpen()) { session.getBasicRemote().sendText(message); } }
public static void main(String[] args) throws Exception { WebSocketContainer container = ContainerProvider.getWebSocketContainer(); ChatClient client = new ChatClient(); container.connectToServer(client, new URI("ws://localhost:8080/chat")); client.sendMessage("Hello, world!"); Thread.sleep(1000); }}2. Tyrus (Reference Implementation)
Tyrus is the reference implementation of JSR 356 from Oracle. It can be used as a standalone library without a full application server. Ideal for microservices and lightweight applications.
Features:
- Works with Grizzly, Jetty, or JDK HTTP server
- Support for SockJS for compatibility with browsers without WebSocket
- Configurable filters and interceptors
- Flexible configuration via XML or Java Config
Standalone server example on Tyrus + Grizzly:
import org.glassfish.tyrus.server.Server;import javax.websocket.*;import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/echo")public class EchoEndpoint {
@OnMessage public String echo(String message) { return "Echo: " + message; }
public static void main(String[] args) { Server server = new Server("localhost", 8025, "/ws", EchoEndpoint.class); try {