A real-time chat API built with FastAPI, Redis, and WebSocket support.
- Real-time chat using WebSocket connections
- Redis-based message storage with automatic expiry
- Room-based chat system
- User management
- Message history support
- Rate limiting
- CORS support
- OpenAPI documentation
- Fixed Redis implementation to use modern
hset
commands - Updated Pydantic model serialization to use
model_dump()
- Improved WebSocket connection handling
- Enhanced test coverage for database operations
chat-api/
├── src/ # Source code
│ ├── api/ # API endpoints
│ │ └── v1/ # API v1 routes
│ ├── config/ # Configuration settings
│ ├── core/ # Core business logic
│ ├── middleware/ # Middleware components
│ ├── models/ # Data models
│ └── services/ # Services (Database, WebSocket)
├── tests/ # Test suite
├── static/ # Static files and documentation assets
├── .env # Environment variables
├── example.env # Example environment variables
├── requirements.txt # Python dependencies
├── setup.py # Package setup
├── pytest.ini # Pytest configuration
└── README.md # Project documentation
- Python 3.8+
- Redis server
- Virtual environment (recommended)
- Clone the repository:
git clone https://github.com/Nuu-maan/chat-api.git
cd chat-api
- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Copy example.env to .env and configure your settings:
cp example.env .env
Edit .env
file to configure:
- Redis connection settings
- API settings
- Rate limiting parameters
- Logging configuration
The following environment variables are required:
REDIS_URL
: Redis connection URL (default:redis://localhost:6379
)MESSAGE_EXPIRY_DAYS
: Number of days to keep messages (default: 7)MAX_MESSAGES_PER_ROOM
: Maximum messages per room (default: 100)RATE_LIMIT_REQUESTS
: Maximum requests per minute (default: 60)
-
Start the Redis server
-
Run the FastAPI server:
uvicorn src.main:app --reload
The API will be available at http://localhost:8000
Visit the beautiful documentation at:
/
- Main documentation with Swagger UI/redoc
- Alternative documentation with ReDoc
ws://localhost:8000/api/v1/ws/{room_id}/{user_id}
{
"type": "text",
"content": "Hello, World!",
"user_id": "user123",
"room_id": "room456"
}
POST /api/v1/rooms
- Create roomGET /api/v1/rooms
- List roomsGET /api/v1/rooms/{room_id}
- Get roomGET /api/v1/rooms/{room_id}/messages
- Get room messages
POST /api/v1/users
- Create userGET /api/v1/users
- List usersGET /api/v1/users/{user_id}
- Get user
POST /api/v1/rooms/{room_id}/messages
- Send message
Run the test suite:
pytest tests/ -v
Contributions are greatly appreciated! Here's how you can help:
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature
- Make your changes and commit them:
git commit -m 'Add some amazing feature'
- Push to the branch:
git push origin feature/amazing-feature
- Open a Pull Request
- Additional features and endpoints
- Performance improvements
- Documentation enhancements
- Bug fixes
- Test coverage improvements
- Security enhancements
- Follow PEP 8 style guide
- Write tests for new features
- Update documentation for changes
- Keep commits atomic and well-described
- Add type hints to new code
This project is licensed under the MIT License - see the LICENSE file for details.
Visit the project repository at https://github.com/Nuu-maan/chat-api