Java WebSocket Library: Tyrus — JSR 356 Implementation for Real-Time

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Java WebSocket Library: Tyrus — Reference Implementation of JSR 356



In modern web development, the need for real-time bidirectional communication has become a standard. Chat applications, online games, trading terminals, and notification systems all require the server to be able to initiate sending data to the client without constant HTTP requests. For the Java ecosystem, the de facto standard is the JSR 356 specification (Java API for WebSocket), and its reference implementation is the Tyrus library.



On the page pythonlib.ru we often review tools for real-time communications. Today we will take a detailed look at Tyrus — a powerful and flexible library for working with WebSocket in Java, which allows you to easily integrate bidirectional data exchange into your projects.



What is Tyrus and why is it needed?



Tyrus is an open-source reference implementation of the JSR 356 specification, developed under the auspices of the Eclipse Foundation. It provides a complete set of APIs for creating both server-side and client-side WebSocket applications in Java. The main advantages of Tyrus:



  • Full compliance with the JSR 356 standard — code written using Tyrus can be easily ported to other implementations (e.g., Apache Tomcat, Jetty).
  • Annotation support — a minimalistic approach: just add the @ServerEndpoint or @ClientEndpoint annotations to a POJO.
  • Flexibility — works both in servlet containers (Tomcat, GlassFish) and in standalone mode (embedded Grizzly server).
  • Security — support for WSS (WebSocket Secure) via standard Java SSL sockets.


Installing and connecting Tyrus



To work with Tyrus, you need to add several dependencies to pom.xml (Maven). The basic configuration includes the library core and the transport layer (Grizzly for standalone mode):



<!-- Main Tyrus dependency --><dependency>    <groupId>org.glassfish.tyrus</groupId>    <artifactId>tyrus-server</artifactId>    <version>1.19</version></dependency>

<!-- Embedded Grizzly server for standalone mode --><dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly-server</artifactId> <version>1.19</version></dependency>

<!-- Client part (if you need to connect to other servers) --><dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-client</artifactId> <version>1.19</version></dependency>


For Gradle (build.gradle):



implementation 'org.glassfish.tyrus:tyrus-server:1.19'implementation 'org.glassfish.tyrus:tyrus-container-grizzly-server:1.19'implementation 'org.glassfish.tyrus:tyrus-client:1.19'


Creating a WebSocket server with Tyrus



The server side is a regular Java class with the @ServerEndpoint annotation. Tyrus automatically manages the connection lifecycle.



import javax.websocket.*;import javax.websocket.server.ServerEndpoint;import java.io.IOException;import java.util.Set;import java.util.concurrent.CopyOnWriteArraySet;

@ServerEndpoint("/chat")public class ChatEndpoint {

// Store all active sessions private static final Set<Session> sessions = new CopyOnWriteArraySet<>();

@OnOpen public void onOpen(Session session) { sessions.add(session); System.out.println("New connection: " + session.getId()); broadcast("User " + session.getId() + " has joined the chat."); }

@OnMessage public void

Recommendations