How prajjwal2051/chat-connect works
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 CLI client · server.py Chat server · to_find_a_free_port.py Free port
Components 14
Client runtime
Interactive terminal chat client
- CLI client · Interactive terminal process that connects to the chat server
clinet.py - CLI flags · Parses host, port, username, password, and color settings
clinet.py - Terminal UI · Formats incoming chat output with timestamps, colors, and screen clearing
clinet.py - Slash commands · Handles in-chat commands such as help, list, clear, and quit
clinet.py
Server runtime
TCP chat server and shared state
- Chat server · Main TCP server that mediates all chat traffic
server.py - Accept loop · Binds host and port, accepts clients, and enforces the max client limit
server.py - Client threads · Runs one thread per connected client
server.py - Admission gate · Checks server password, username regex rules, and duplicate names
server.py - Session state · Tracks connected users and active chat sessions in memory
server.py - Message rules · Applies rate limiting and the 500-character message cap
server.py - Broadcast hub · Distributes chat, join, and leave events to connected clients
server.py - Shutdown · Handles Ctrl+C, notifies clients, and closes sockets cleanly
server.py - Logging · Provides colored runtime logging and optional file output
server.py
Support tools
Local setup and operational helpers
- Free port · Prints an available local port for setup and deployment
to_find_a_free_port.py
Connections 15
- 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