Netty Project | Understanding Netty using simple real-world example of Chat server client | Good for beginners

In this article we will understand Netty in simplistic way with real world example of Chat server client example.

What Netty is or is not

  • Netty is not a traditional web server by itself like tomcat or jboss etc. Netty is a NIO based client server framework. This means that it provides a simplified layer over plain NIO networking which makes it easy to create low level networking application.
  • Netty is not a servlet-jsp container server or basic web server. But Netty does support HTTP protocol & its possible to create a web server using Netty as base.
  • Netty can be used to create both sides of networking based application i.e. server as well as client.
  • Netty is non-blocking ex: when client sends a message to server, that call immediately returns & client code can move further. Client can provide Callback-style handler which will handle response from server whenever received from server.
  • Netty runs embedded within code ex. it needs a class with main() method to bootstrap.
  • Netty is event based. Once server client connection is established, communication happens in form of events. Channel handlers can be coded to handle these events & process them.



Netty components

In short & on high level, Netty server or client needs these basic classes.

  • Main class – This class bootstraps server or client. In case of server, it will start server at given host/port. In case of client, it connects to provided server & creates connection.
  • Channel Handlers – Channel handlers handles different events like channel active, channel read or exception caught etc.

Chat Server-Client application using Netty

Lets create a Chat application explained here.

  • Create console input based chat client application.
  • On start of client, we will ask his name & afterwards get a chat message to send to other clients.
  • Send name & chat message to chat server.
  • Chat Server will then publish that message to all clients along with the name of the sender.




Here is the diagram which shows components of server & client. Below diagram also traces the communication path of a chat message “Hello” from one client to another.

Lets code

Dependency – To use Netty server client framework, Netty dependency is required which is available in maven repository at netty-all

Bootstraping Netty

  • ServerBootstrap – Code to bootstrap server channel & start server. It binds the server to the port on which it will listen for connection.
  • Bootstrap – Code to bootstrap client channel.
  • EventLoopGroup
    • Group of EventLoop. EventLoop handles all the I/O operations for a Channel once registered.
    • ServerBootstrap needs 2 types of EventLoopGroup i.e. “boss” & “worker”. Client bootstrap doesn’t need boss group.
    • Boss EventLoopGroup – This event loop group looks for & accepts incoming connections.
    • Worker EventLoopGroup – Boss accepts connection & registers it to worker EventLoopGroup.  Worker handles all the events during the communication through that connection.
  • Channel NioServerSocketChannel – Configure server to use NIO selector based implementation to accept new connections.
  • Decoder/encoder (StringDecoder / StringEncoder)
    • Netty communication occurs over network socket channel which is in byte format. So in case we want to send specific data types, then we provide encoder to encode data type into bytes & decode bytes to data type.
    • String encoder, decoders are provided by Netty. But we can create our own encoders or decoders for any data type that we need.



Chat Server & handler

Here is the Server bootstrap code.




Here is the handler for chat server.



Chat client & handler

Here is the client bootstrap code.

Here is custom channel handler to print chat messages for client.



Execute Netty chat application

Here are the steps to run this example.

  • First run ChatServer.java as Java Application. Wait till it starts & is ready to accept client. Look for message in logs.
  • Once server is up, run ChatClient.java as Java application. Provide name of first chat user once asked in console.
  • Again start another instance of ChatClient.java as Java application & provide name of second chat user once asked in console.
  • Then exchange chat messages through console input & see message delivered to other clients including that client.

ChatServer console

First ChatClient console

User entered inputs are highlighted.

Second ChatClient console

User entered inputs are highlighted.

As you can see in above console logs, 2 chat users connected to chat server & exchanged messages with each other.

You can find complete code in GIT Repository.



Further improvements

This example is very simple & focuses mainly on Netty concepts instead of usability of chat. Further improvements can be done in this example to improve & try hands on. Here are some improvements that you can try.

  • Currently we send chat message to all clients. Add code to select user with which user wants to chat. Send chat message only to that user.
  • Currently we are only exchanging String messages using String encoder & decoder. Write your own encoder & decoder & try sending some other objects or data like file, image etc.



Leave a Reply

Your email address will not be published.