Coder Social home page Coder Social logo

nio-chat's Introduction

NIO-CHAT

A chat server and client implementation using the Java NIO package. Implements the event-based selector pattern which is a neat way to avoid creating a new thread for every client connection.As a result this will scale much better than the traditional thread-per-connection approach.

This chat server and client were written for an Android demo project but can easily be updated to work in a production environment.

If you plan to use this code there's a few things for you to consider:

  • buffer size as specified in ChatConfig
  • Message delimiter (also in ChatConfig). Currently a CRLF is used but this might not be appropriate for your application. Either way, a delimiter is needed so that clients can breakdown the payload from the server into individual messages as sent by other clients.
  • messages received by the server are currently delivered on the selector thread, which is fine for a small amount of clients but will become a bottleneck as the clients increase. You can easily introduce a java.util.concurrent.ThreadPoolExecutor in the ChatServer which can deliver messages and avoid blocking the selector thread for too long.
  • error handling could be improved. One option would be to add an onException() method to the ChatResponseHandler.
  • You will need to use a CharSet that works for both the client and server. UTF-8 is a good choice for Western languages while UTF-16 might be a better choice for East Asian language

nio-chat's People

Contributors

mpkelly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.