Building an Internet Chat System with Netty, WebSocket, and Spring Integration
This article explains how to create a web‑based chat application similar to WeChat using Netty as the core server, WebSocket for real‑time communication, and Spring MVC for HTTP handling, covering architecture, required tools, core code, and deployment steps.
0. Introduction
The goal is to build an Internet chat system, comparable to the web version of WeChat, using Netty as the core framework and WebSocket as the application‑layer protocol. Only essential features such as one‑to‑one chat, group chat, and file transfer are implemented, and the project is integrated with Spring to be open‑source and extensible.
About Netty
Netty is a Java client/server framework that abstracts low‑level network operations and provides an easy‑to‑use API.
About WebSocket
WebSocket solves the limitation of HTTP where only the client can initiate communication; it uses an HTTP handshake to upgrade the connection and then enables lightweight, efficient, bidirectional text transmission.
1. Technical Preparation
IDE: MyEclipse 2016
JDK: 1.8.0_121
Browsers: Google Chrome, 360 Browser (speed mode)
Key technologies: Netty 4, WebSocket + HTTP, Spring MVC + Spring, jQuery, Bootstrap 3 + bootstrap‑fileinput, Maven 3.5, Tomcat 8.0
2. Overall Design
2.1 Design Idea
Tomcat runs on port 8080 handling HTTP requests such as login and user information, while a separate thread runs a Netty WebSocket server on port 3333 for real‑time message exchange. After a user logs in, the browser keeps a Session (30‑minute timeout). Tomcat returns user data and stores the user's WebSocket connection for later messaging. When a user sends a message, the server looks up the recipient’s connection and forwards the message. On logout, both the Session and the WebSocket connection are released to avoid memory leaks.
2.2 System Architecture
The system follows a B/S model and adopts an MVC‑like layering: View, Controller, Service, Model, and Data Access.
2.3 Project Structure
Backend and frontend directory structures are illustrated in the following images.
2.4 System Modules
Login module – uses a simple Session mechanism for authentication and releases the WebSocket connection on logout.
Chat management module – core functionality built with Netty, supporting single and batch text/file messages and emojis.
Other modules (friend management, chat history, registration) are not implemented but can be added.
2.5 User State Diagram
2.6 User Interface
Sample chat UI screenshots:
3. Core Code
The following Java class shows how to start and gracefully shut down the Netty WebSocket server. The full source is available in the repository.
/**
* 描述: Netty WebSocket服务器
* 使用独立的线程启动
* @author Kanarien
* @version 1.0
* @date 2018年5月18日 上午11:22:51
*/
public class WebSocketServer implements Runnable{
/**
* 描述:启动Netty Websocket服务器
*/
public void build() {
// 略,详细请看源码
}
/**
* 描述:关闭Netty Websocket服务器,主要是释放连接
* 连接包括:服务器连接serverChannel,
* 客户端TCP处理连接bossGroup,
* 客户端I/O操作连接workerGroup
*
* 若只使用
* bossGroupFuture = bossGroup.shutdownGracefully();
* workerGroupFuture = workerGroup.shutdownGracefully();
* 会造成内存泄漏。
*/
public void close(){
serverChannelFuture.channel().close();
Future
bossGroupFuture = bossGroup.shutdownGracefully();
Future
workerGroupFuture = workerGroup.shutdownGracefully();
try {
bossGroupFuture.await();
workerGroupFuture.await();
} catch (InterruptedException ignore) {
ignore.printStackTrace();
}
}
}4. Demonstration
4.1 Login
Login URLs:
http://localhost:8080/WebSocket/login or http://localhost:8080/WebSocket/
There are nine preset users (Member001‑Member009) each with password equal to the numeric suffix.
4.2 Chat Demo
After logging in, users can exchange messages, files, and emojis in real time.
5. Source Code Download
Download link: https://pan.baidu.com/s/1uLyUXvQKWL09URNsLEOdHw?pwd=654k
Extraction code: 654k
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.