Backend Development 28 min read

Design and Implementation of a Netty‑Based Chat System with SpringBoot, JavaFX and Shiro

This article presents a complete tutorial on building a TCP‑based instant‑messaging system using Netty for the server, SpringBoot for the web admin console, JavaFX for the desktop client, Shiro for authentication, and MySQL for user data, covering architecture, protocol design, and full source code examples.

Java Captain
Java Captain
Java Captain
Design and Implementation of a Netty‑Based Chat System with SpringBoot, JavaFX and Shiro

1. Introduction

The author explains the motivation behind writing a simple chat system, sets a playful goal of gaining comments for future Netty articles, and briefly mentions the project’s three main components: a chat server, a chat client, and a web management console.

2. Final Effect

Three parts are shown: the server (receives and forwards messages), the client (login, send and receive chat messages), and the web console (user management). Screenshots of log output and UI are provided.

2.1 Chat Server

The server receives heart‑beat packets and chat messages, then forwards them to the target user. Sample log output is displayed:

2021-05-11 10:41:40.037  INFO 9392 --- [ntLoopGroup-3-1] c.e.o.s.netty.handler.HeartBeatHandler   : server收到心跳包:{"time":1620700900029,"messageType":"99"}
2021-05-11 10:41:50.049  INFO 9392 --- [ntLoopGroup-3-1] c.e.o.s.n.handler.BussMessageHandler   : 收到消息:{"time":1620700910045,"messageType":"14","sendUserName":"guodegang","recvUserName":"yuqian","sendMessage":"于老师你好"}
...

2.2 Chat Client

The client handles login, sending messages, and receiving messages. Two client windows are demonstrated logging in with different users.

2.3 Web Management Console

A simple SpringBoot‑based admin page is built with Layui‑Mini for the UI and Shiro for authentication, currently only showing user account management.

3. Requirement Analysis

No additional functional requirements beyond those demonstrated in section 2.

4. High‑Level Design

4.1 Technology Selection

Chat Server : TCP long‑connection, full‑duplex communication, implemented with Netty.

Web Console : SpringBoot backend, Layui‑Mini frontend, Spring MVC + JPA + Shiro.

Chat Client : SpringBoot + JavaFX (desktop UI).

Build Tool : Maven.

4.2 Database Design

A single sys_user table stores user credentials, status, and online flag. The DDL is provided.

4.3 Communication Design

The protocol consists of an 8‑byte length header followed by a JSON body. The header solves TCP packet‑sticking problems. Message types are identified by a 2‑character prefix (e.g., "01" for login request).

Typical interaction scenarios (login, send message success, target offline, send failure) are illustrated with sequence diagrams.

5. Implementation

5.1 Netty Basics

Netty uses a pipeline of handlers. Inbound handlers process received data; outbound handlers process data to be sent. The article lists three inbound handlers (byte‑to‑String, JSON‑to‑Java bean, business logic) and two outbound handlers (Java bean‑to‑JSON, String‑to‑bytes).

5.2 Server Code

The server bootstrap creates boss and worker groups, configures the pipeline with IdleStateHandler , StringLengthFieldDecoder , StringDecoder , JsonDecoder , HeartBeatHandler , JsonEncoder , and BussMessageHandler . Core handlers are shown:

public void start(){
    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup worker = new NioEventLoopGroup();
    ServerBootstrap sb = new ServerBootstrap();
    sb.group(boss, worker)
      .channel(NioServerSocketChannel.class)
      .childHandler(new ChannelInitializer
(){
          protected void initChannel(SocketChannel ch){
              ch.pipeline().addLast(new IdleStateHandler(25,20,0,TimeUnit.SECONDS));
              ch.pipeline().addLast(new StringLengthFieldDecoder());
              ch.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
              ch.pipeline().addLast(new JsonDecoder());
              ch.pipeline().addLast(new HeartBeatHandler());
              ch.pipeline().addLast(new JsonEncoder());
              ch.pipeline().addLast(bussMessageHandler);
          }
      });
    // bind and wait …
}

Key handlers such as HeartBeatHandler , StringLengthFieldDecoder , JsonDecoder , BussMessageHandler , SendMsgExecutor , LoginExecutor , and JsonEncoder are explained with code snippets and comments.

5.3 Client Code

The client uses a similar Netty pipeline, adds IdleStateHandler , StringLengthFieldDecoder , StringDecoder , JsonDecoder , JsonEncoder , BussMessageHandler , and HeartBeatHandler . After establishing the connection, it sends a LoginRequest and handles the LoginResponse to update UI via JavaFX.

5.4 Web Management End

Shiro authentication is set up with a custom UserDbRealm . The filter chain permits static resources anonymously and protects all other URLs. Login and query APIs are implemented with Spring MVC, using a generic QueryService that builds JPA specifications from request parameters.

6. Conclusion

The article provides a complete, runnable example of a Netty‑based instant messaging system, covering server, client, and admin console, and encourages readers to type the code themselves for deeper understanding.

NettyTCPSpringBootChat ApplicationshiroJavaFX
Java Captain
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.