Project Structure/
├── app/
│ ├── __init__.py
│ ├── config.py
│ ├── db.py
│ ├── logger.py
│ ├── models.py
│ ├── server.py
│ └── utils.py
├── client/
│ ├── __init__.py
│ └── cli.py
├── tests/
│ └── test_models.py
├── .env.example
├── .gitignore
├── requirements.txt
├── README.md
├── start_server.sh
├── start_client.sh
└── docker-compose.yml Async WebSocket chat server + minimal CLI client using Python asyncio, websockets and MongoDB.
-
Copy
.env.exampleto.envand edit if needed. -
Start MongoDB:
- Locally:
docker-compose up -d mongoOR rundocker-compose up -dto bring up mongo.
- Locally:
-
Create virtualenv and install deps:
python -m venv .venv source .venv/bin/activate pip install -r requirements.txt
Start the Server:
./start_server.sh
In another terminal, start client:
./start_client.sh
Tests:
pytest
Protocol (JSON)
register: {"type":"register","username":"..."}
message: {"type":"message","room":"room","content":"text"}
create_room / join_room / leave_room / list_rooms / list_users / history
Next improvements
Add JWT/auth
Dockerize server + CI
Add pytest-asyncio integration tests
- Things to fix/add (priority):
- Auth — right now anyone can register as anyone. Will add tokens/JWT to prevent impersonation and secure WebSocket handshake.
- Connection health — heartbeats/pings and reconnect logic for clients.
- Error handling & logging — more structured logs, maybe Sentry for prod.
- Testing — integration tests with test Mongo and CI.
- Scaling — in-memory maps (room_members, active_connections) won’t work across multiple server instances. Add Redis/pubsub for horizontal scaling later.
- Rate limiting per-IP — consider abusive clients that create many usernames.