Netty websocket chat

Daniel Zielinski
6 min readMar 17, 2023

We will create simple chat application with Java and Netty!!! Our chat will consist of server and client. All configuration will be hardcoded for better understanding.

Websocket chat server

Consist of three classes:

  • WebsocketServer — main java class, server configuration
  • WebSocketServerInitalizer — implementation of ChannelInitializer, piplines configuration
  • WebSocketServerHandler — implementation of SimpleChannelInboundHandler, our business logic.
@Slf4j
public class WebsocketServer {

public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
log.info("Starting websocket chat server........");
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new WebSocketServerInitializer());
ChannelFuture chf = b.bind(5544).sync();
log.info("!!!!Netty websocket chat server is Alive!!!!");
chf.channel().closeFuture().sync();

} finally {
log.info("Shutting down websocket chat server........");
bossGroup.shutdownGracefully()…

--

--