WebSocket is one of the core technologies behind modern real-time web applications such as chat systems, live dashboards, online games, and streaming services.
In this article, I’d like to introduce a small open-source project I created:
👉 GitHub
https://github.com/oyahiroki/hello-java-websocket
This project demonstrates how to build a simple WebSocket application in Java using an embedded Jetty server, along with both browser-side and Java-side WebSocket clients.
Why WebSocket?
Traditional HTTP communication works in a request-response style:
- Client sends a request
- Server returns a response
- Connection closes
For real-time systems, this model becomes inefficient because clients must continuously poll the server.
WebSocket solves this problem by enabling:
- Persistent connections
- Bidirectional communication
- Real-time message delivery
- Reduced network overhead
Typical use cases include:
- Chat applications
- Notification systems
- Real-time monitoring dashboards
- Multiplayer games
- AI streaming responses
- IoT systems
Project Overview
The project is intentionally simple and easy to understand.
It includes:
- Java WebSocket server
- Browser-based JavaScript client
- Standalone Java WebSocket client
- Embedded Jetty server
- Maven build configuration
The goal is educational clarity rather than framework complexity.
Technology Stack
This project uses lightweight and standard technologies:
| Technology | Purpose |
|---|---|
| Java 8+ | Core language |
| Maven | Build and dependency management |
| Jetty 9.4 | Embedded web server |
| Tyrus | Java WebSocket reference implementation |
| JSP | Web UI |
| jQuery | Simple browser interaction |
The implementation is based on the Java WebSocket API (JSR 356).
Quick Start
1. Clone the Repository
git clone https://github.com/oyahiroki/hello-java-websocket.git
cd hello-java-websocket
2. Start the Server
mvn jetty:run
The server starts at:
http://localhost:8080/hello-websocket
3. Open the Browser Client
Access:
http://localhost:8080/hello-websocket/index.jsp
You can send messages directly from the browser and observe real-time WebSocket communication.
WebSocket Endpoint
The WebSocket endpoint is:
ws://localhost:8080/hello-websocket/helloendpoint
This allows both browser clients and Java clients to communicate with the same server.
Project Structure
hello-java-websocket/
├── src/
│ └── main/
│ ├── java/
│ │ ├── WebSocket server classes
│ │ └── Java WebSocket client
│ └── webapp/
│ └── index.jsp
├── pom.xml
└── README.md
The structure is intentionally minimal so beginners can easily understand the flow.
Browser-Based WebSocket Communication
The browser client uses JavaScript WebSocket APIs.
Example:
const ws = new WebSocket("ws://localhost:8080/hello-websocket/helloendpoint");
ws.onopen = function() {
console.log("Connected");
ws.send("Hello WebSocket!");
};
ws.onmessage = function(event) {
console.log("Received:", event.data);
};
This demonstrates the essential WebSocket lifecycle:
- Connect
- Send
- Receive
- Close
Java WebSocket Client
The project also includes a standalone Java client.
This is useful for:
- Backend integrations
- Automated testing
- IoT communication
- Batch systems
- Server-to-server communication
Example execution:
pkg.WebSocketClientMain.main()
Why Embedded Jetty?
One of the nice aspects of this project is that it does not require deploying to a heavyweight application server.
Using embedded Jetty provides:
- Fast startup
- Simple development workflow
- Easy portability
- Lightweight architecture
You can run everything with a single Maven command.
Educational Focus
Many WebSocket tutorials today rely on large frameworks such as:
- Spring Boot
- Node.js ecosystems
- Complex frontend frameworks
Those are useful in production, but sometimes they hide the underlying WebSocket concepts.
This project focuses on:
- Understanding the protocol
- Learning basic server/client communication
- Minimal dependencies
- Clear architecture
It is especially useful for:
- Java beginners
- Students
- Engineers learning real-time systems
- Developers experimenting with streaming applications
Potential Extensions
This small example can be extended into larger systems:
- Authentication
- Multi-user chat
- Real-time dashboards
- AI streaming interfaces
- Event-driven systems
- Monitoring tools
Because the codebase is intentionally clean, it is easy to customize.
GitHub Repository
The full source code is available here:
👉 https://github.com/oyahiroki/hello-java-websocket
If you are learning Java WebSocket programming, I hope this project helps you get started quickly.
⭐ GitHub stars are always appreciated!
License
This project is released under the Apache License 2.0.
Note: This article was created using a personal IBM Bob account. The content is entirely original.