# prajjwal2051/chat-connect architecture

> Architecture diagram of the GitHub repository prajjwal2051/chat-connect, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/prajjwal2051/chat-connect
- Repository: https://github.com/prajjwal2051/chat-connect

## Overview

1. **Overall shape**
   - This is a **two-process, client/server CLI chat system** built with the Python **standard library only**.
   - The architecture is centered on a single TCP server that mediates all chat traffic between multiple terminal clients.

2. **Server process (`server.py`)**
   - The server binds to a configurable **host/port**, accepts incoming TCP connections, and enforces a **maximum client limit**.
   - It is the authoritative boundary for chat membership, authentication, message distribution, and shutdown coordination.

3. **Concurrent connection model**
   - The server is **multi-threaded**, with **one thread per connected client**.
   - This means each client connection is handled independently while still sharing the same in-memory server state for users, sessions, and broadcast fan-out.

4. **Admission control and authentication**
   - Before joining the chat, a client must provide the correct **server password**.
   - The server also prevents **duplicate usernames** and validates usernames with a regex rule.
   - This establishes a clear trust boundary: only authenticated, unique users enter the live chat set.

5. **Chat message flow**
   - Once admitted, a client sends text messages over TCP to the server.
   - The server applies enforcement rules like **rate limiting** and a **500-character message cap**, then broadcasts accepted messages to the other connected clients.
   - Join/leave events are also emitted through the same server-mediated distribution path.

6. **Client process (`clinet.py`)**
   - The client is an interactive **terminal UI** that connects to the server, prompts for credentials when needed, and renders incoming chat output.
   - It supports CLI flags for host, port, password, username, and color toggling, so the runtime can be configured either interactively or non-interactively.

7. **Terminal presentation layer**
   - The client formats chat output with **ANSI colors** and **HH:MM timestamps**.
   - The UI also handles local terminal actions such as **clearing the screen** and graceful exit behavior.

8. **Slash-command behavior**
   - The app exposes in-chat commands such as `/help`, `/list`, `/clear`, and `/quit`.
   - These commands are part of the chat interaction model rather than separate tools, letting users request help, inspect active users, clear the display, or disconnect cleanly.

9. **Logging and observability**
   - The server provides **colored structured logging** for runtime events.
   - It can optionally write logs to a file, which makes the server the main observability point for authentication attempts, joins/leaves, and operational events.

10. **Shutdown and lifecycle**
   - The server is designed for **graceful shutdown on Ctrl+C**: it notifies connected clients and closes sockets cleanly.
   - This is important because the server owns the connection lifecycle and must actively terminate all active client sessions.

11. **Port discovery utility (`to_find_a_free_port.py`)**
   - This small helper exists to avoid hardcoded port conflicts by printing an available OS port.
   - It is operational support tooling, not part of the chat runtime itself, but it affects deployment and local setup.

12. **External dependencies and deployment boundary**
   - The project has **no third-party runtime dependencies**; `requirements.txt` mainly documents that the standard library is sufficient.
   - There is no database, message broker, or external service integration: all state is in-process on the server and all communication is direct TCP between clients and server.

13. **How to draw the diagram**
   - Model it as: **multiple CLI clients → single TCP server → broadcast back to clients**.
   - Put authentication, validation, rate limiting, user/session tracking, and logging inside the server boundary.
   - Put terminal rendering, prompts, and slash-command input handling inside the client boundary.

Main entry points: [`clinet.py`](https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py) (CLI client), [`server.py`](https://github.com/prajjwal2051/chat-connect/blob/main/server.py) (Chat server), [`to_find_a_free_port.py`](https://github.com/prajjwal2051/chat-connect/blob/main/to_find_a_free_port.py) (Free port)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client runtime"]
  node_client_app["CLI client<br/>terminal app<br/>[clinet.py]"]
  node_client_args["CLI flags<br/>config/input<br/>[clinet.py]"]
  node_client_ui["Terminal UI<br/>presentation<br/>[clinet.py]"]
  node_client_cmds["Slash commands<br/>chat commands<br/>[clinet.py]"]
end

subgraph group_server["Server runtime"]
  node_server_app[("Chat server<br/>tcp server<br/>[server.py]")]
  node_server_accept["Accept loop<br/>connection handling<br/>[server.py]"]
  node_server_threads["Client threads<br/>concurrency<br/>[server.py]"]
  node_server_auth["Admission gate<br/>auth and validation<br/>[server.py]"]
  node_server_state["Session state<br/>membership store<br/>[server.py]"]
  node_server_rules["Message rules<br/>enforcement<br/>[server.py]"]
  node_server_bus["Broadcast hub<br/>message fan-out<br/>[server.py]"]
  node_server_shutdown["Shutdown<br/>lifecycle<br/>[server.py]"]
  node_server_log["Logging<br/>observability<br/>[server.py]"]
end

subgraph group_support["Support tools"]
  node_free_port["Free port"]
end

node_client_app -->|"reads options"| node_client_args
node_client_app -->|"renders"| node_client_ui
node_client_app -->|"accepts commands"| node_client_cmds
node_client_app -->|"TCP chat"| node_server_app
node_server_app -->|"accepts"| node_server_accept
node_server_accept -->|"spawns"| node_server_threads
node_server_threads -->|"gates entry"| node_server_auth
node_server_auth -->|"admits users"| node_server_state
node_server_state -->|"scans messages"| node_server_rules
node_server_rules -->|"passes accepted chat"| node_server_bus
node_server_auth -->|"join/leave events"| node_server_bus
node_server_bus -->|"delivers output"| node_client_app
node_server_app -->|"logs events"| node_server_log
node_server_app -->|"terminates cleanly"| node_server_shutdown
node_free_port -.->|"setup aid"| node_server_app

click node_client_app "https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py"
click node_client_args "https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py"
click node_client_ui "https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py"
click node_client_cmds "https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py"
click node_server_app "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_accept "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_threads "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_auth "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_state "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_rules "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_bus "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_shutdown "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_server_log "https://github.com/prajjwal2051/chat-connect/blob/main/server.py"
click node_free_port "https://github.com/prajjwal2051/chat-connect/blob/main/to_find_a_free_port.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_app,node_client_args,node_client_ui,node_client_cmds toneBlue
class node_server_app,node_server_accept,node_server_threads,node_server_auth,node_server_state,node_server_rules,node_server_bus,node_server_shutdown,node_server_log toneAmber
class node_free_port toneMint
```

## Components

### Client runtime

Interactive terminal chat client

- [CLI client](https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py): terminal app · Interactive terminal process that connects to the chat server · `clinet.py`
- [CLI flags](https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py): config/input · Parses host, port, username, password, and color settings · `clinet.py`
- [Terminal UI](https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py): presentation · Formats incoming chat output with timestamps, colors, and screen clearing · `clinet.py`
- [Slash commands](https://github.com/prajjwal2051/chat-connect/blob/main/clinet.py): chat commands · Handles in-chat commands such as help, list, clear, and quit · `clinet.py`

### Server runtime

TCP chat server and shared state

- [Chat server](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): tcp server · Main TCP server that mediates all chat traffic · `server.py`
- [Accept loop](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): connection handling · Binds host and port, accepts clients, and enforces the max client limit · `server.py`
- [Client threads](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): concurrency · Runs one thread per connected client · `server.py`
- [Admission gate](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): auth and validation · Checks server password, username regex rules, and duplicate names · `server.py`
- [Session state](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): membership store · Tracks connected users and active chat sessions in memory · `server.py`
- [Message rules](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): enforcement · Applies rate limiting and the 500-character message cap · `server.py`
- [Broadcast hub](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): message fan-out · Distributes chat, join, and leave events to connected clients · `server.py`
- [Shutdown](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): lifecycle · Handles Ctrl+C, notifies clients, and closes sockets cleanly · `server.py`
- [Logging](https://github.com/prajjwal2051/chat-connect/blob/main/server.py): observability · Provides colored runtime logging and optional file output · `server.py`

### Support tools

Local setup and operational helpers

- [Free port](https://github.com/prajjwal2051/chat-connect/blob/main/to_find_a_free_port.py): utility · Prints an available local port for setup and deployment · `to_find_a_free_port.py`

## Connections

- CLI client → CLI flags: reads options · Uses CLI flags or interactive prompts to configure the session
- CLI client → Terminal UI: renders · Displays incoming chat output and local terminal actions
- CLI client → Slash commands: accepts commands · Processes slash commands entered by the user
- CLI client → Chat server: TCP chat · Connects to the server and exchanges messages over TCP
- Chat server → Accept loop: accepts · Listens for incoming client connections
- Accept loop → Client threads: spawns · Creates one handler thread per client connection
- Client threads → Admission gate: gates entry · Validates password and username before joining chat
- Admission gate → Session state: admits users · Adds accepted clients to the in-memory session set
- Session state → Message rules: scans messages · Shared server state is consulted while enforcing message rules
- Message rules → Broadcast hub: passes accepted chat · Accepted messages become broadcast events
- Admission gate → Broadcast hub: join/leave events · Membership changes are distributed through the same fan-out path
- Broadcast hub → CLI client: delivers output · Sends chat messages and system events back to clients
- Chat server → Logging: logs events · Records authentication, joins, leaves, and operational activity
- Chat server → Shutdown: terminates cleanly · Stops accepting clients and notifies active sessions on exit
- Free port → Chat server: setup aid · Provides an available port for launching the server
