🐸 pepe:curious

building a scalable chat application in c++ Using async io

πŸ“œ Project Overview

chatRoomCpp is a simple yet powerful chat room application implemented in C++, leveraging the Boost.Asio library for asynchronous network communication and threading for managing concurrent client sessions.

What it can do:-


🌟 Key Features


πŸ›οΈ Architecture Overview

The project is structured around a few key classes:

Component Responsibility
Session Represents a client session. Manages socket communication.
Room Manages multiple client sessions. Broadcasts messages.
Message Handles encoding/decoding of chat messages.
Server Accepts incoming client connections.
Client Connects to server, handles user input/output.

Each client connects to the server, joins a room, and can send and receive broadcasted messages asynchronously.


πŸ”₯ Some Functionalities Explained

1. Asynchronous I/O

Example Principle:

boost::asio::async_read(socket, buffer, completion_handler);
boost::asio::async_write(socket, buffer, completion_handler);

2. TCP Sockets

Example Principle:

boost::asio::ip::tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), port));
acceptor.async_accept(new_socket, accept_handler);

3. Threading Model


4. Message Management


🧠 In-Depth: Boost.Asio Concepts Used

Boost.Asio Concept Usage
async_read_until() Read data asynchronously until a delimiter or length is reached.
async_write() Write buffers asynchronously.
io_context.post() Post functions to run in the I/O context's event loop.
Completion Handlers Custom callbacks that execute after I/O operations complete.

Important Practice:


πŸ“ Low-Level Design (LLD)

LLD:-

chatroom lld


πŸ”₯ Message Flow Explained

1. Client takes user input (e.g., a chat message).
2. Client asynchronously writes to socket.
3. Server reads data from socket asynchronously.
4. Server parses the incoming data into a Message object.
5. Server uses Room to broadcast Message to all Sessions.
6. Each Session queues the Message and asynchronously writes it to the respective client's socket.

πŸ› οΈ How to Build and Run

  1. Clone the repository:

    git clone https://github.com/singhdevhub-lovepreet/chatRoomCpp.git
    cd chatRoomCpp
    
  2. Build the project:

    make
    
  3. Start the server:

    ./chatApp <port>
    
  4. Start the client (in separate terminal):

    ./clientApp <port>
    

🧩 Advanced Coding Practices Highlighted


✨ Conclusion

The chatRoomCpp project brilliantly showcases how to design an asynchronous, multithreaded, scalable TCP server in modern C++.
If you're diving into network programming, real-time systems, or low-level system design, studying and extending this project will level up your engineering skills.


πŸ“– Check out the full code here: chatRoomCpp GitHub Repository