A progressive TCP chat server built in Python demonstrating the evolution of concurrency models — from threads to asynchronous event-driven architecture.
This repository showcases backend engineering fundamentals applied to networking systems.
sock_chat is a multi-version networking project designed to explore:
- Low-level TCP socket programming
- Client-server architecture
- Thread-based concurrency
- I/O multiplexing with
select - Broadcast message systems
- Asynchronous programming with
asyncio - Event-loop based architecture
Each version improves scalability and architectural maturity.
Thread-per-client → select() multiplexing → broadcast system → asyncio event loop
Concurrency: OS threads Model: Blocking I/O
Flow:
accept()
└── spawn thread
└── recv() loop
✔ Simple ❌ Thread overhead ❌ Limited scalability
Concurrency: Single-threaded Model: Manual event loop
Core pattern:
select.select(sockets_list, [], sockets_list)✔ Lower memory usage ✔ Better scalability ✔ Event-driven architecture
Adds real chat-room behavior.
for client_sock in clients:
if client_sock != sender:
client_sock.send(...)✔ Multi-client messaging ✔ Shared communication channel ✔ Foundation for real-time systems
Concurrency: Coroutine-based Event Loop: Native asyncio Architecture: Fully non-blocking
Core setup:
server = await asyncio.start_server(handle_client, '127.0.0.1', 8080)- Non-blocking socket handling
- Coroutine scheduling
- Line-based message framing (
readline()) - Backpressure control (
await writer.drain()) - Graceful connection cleanup
- Native event loop management
This mirrors modern backend systems like:
- Async web servers
- High-performance APIs
- Real-time messaging systems
| Version | Model | Threads | Event Loop | Broadcast | Scalability |
|---|---|---|---|---|---|
| v1 | Thread-per-client | ✅ | ❌ | ❌ | Medium |
| v2 | select() | ❌ | Manual | ❌ | Higher |
| v3 | select() + broadcast | ❌ | Manual | ✅ | Higher |
| v4 | asyncio | ❌ | Native | ✅ | Very High |
- Python 3.8+
cd v4
python server.pyExpected output:
Servidor asyncio iniciado en ('127.0.0.1', 8080)
Using telnet:
telnet 127.0.0.1 8080Or:
python client.pyOpen multiple terminals to simulate concurrent users.
Client A sends:
Hello
Client B receives:
('127.0.0.1', 52344) dice: Hello
The sender does not receive its own message.
- TCP stream communication
- Socket lifecycle management
- Blocking vs non-blocking I/O
- Thread scheduling vs event loop scheduling
- I/O multiplexing
- Async/await patterns
- Broadcast messaging systems
- Resource cleanup & error handling
- Backpressure management
- No authentication
- No nickname system
- No TLS encryption
- No command parsing
- No persistent storage
- No rate limiting
- Nickname handshake system
- Command parsing (
/quit,/list,/pm) - TLS support
- Logging & chat history
- Benchmark comparison between versions
- Docker containerization
- Load testing
- WebSocket gateway
- Selector/epoll implementation
This repository demonstrates progressive backend engineering maturity:
- Understanding concurrency trade-offs
- Migrating from threads to event-driven architecture
- Managing real-time connections
- Designing scalable network services
It is suitable as:
- Networking learning project
- Backend fundamentals showcase
- Systems programming portfolio piece
- Concurrency model comparison study
Educational project. Free to use and modify.