The Chat API provides real-time messaging capabilities using WebSocket connections and Redis for data persistence. This document outlines the available endpoints and their usage.
http://localhost:8000/api/v1
Currently, the API uses simple user IDs for authentication. Future versions will implement proper authentication mechanisms.
POST /roomsRequest body:
{
"name": "General Chat"
}Response:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "General Chat",
"created_at": "2024-01-01T12:00:00"
}GET /roomsResponse:
[
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"name": "General Chat",
"created_at": "2024-01-01T12:00:00"
}
]POST /usersRequest body:
{
"username": "john_doe"
}Response:
{
"id": "550e8400-e29b-41d4-a716-446655440001",
"username": "john_doe",
"created_at": "2024-01-01T12:00:00"
}GET /usersResponse:
[
{
"id": "550e8400-e29b-41d4-a716-446655440001",
"username": "john_doe",
"created_at": "2024-01-01T12:00:00"
}
]POST /rooms/{room_id}/messagesRequest body:
{
"content": "Hello, world!",
"user_id": "550e8400-e29b-41d4-a716-446655440001",
"type": "text"
}Response:
{
"id": "550e8400-e29b-41d4-a716-446655440002",
"room_id": "550e8400-e29b-41d4-a716-446655440000",
"user_id": "550e8400-e29b-41d4-a716-446655440001",
"content": "Hello, world!",
"type": "text",
"created_at": "2024-01-01T12:00:00"
}GET /rooms/{room_id}/messagesQuery parameters:
limit(optional): Maximum number of messages to return (default: 50)
Response:
[
{
"id": "550e8400-e29b-41d4-a716-446655440002",
"room_id": "550e8400-e29b-41d4-a716-446655440000",
"user_id": "550e8400-e29b-41d4-a716-446655440001",
"content": "Hello, world!",
"type": "text",
"created_at": "2024-01-01T12:00:00"
}
]ws://localhost:8000/api/v1/ws/{room_id}/{user_id}
Upon connection, the server sends a history message with recent chat messages:
{
"type": "history",
"messages": [
{
"id": "550e8400-e29b-41d4-a716-446655440002",
"room_id": "550e8400-e29b-41d4-a716-446655440000",
"user_id": "550e8400-e29b-41d4-a716-446655440001",
"content": "Hello, world!",
"type": "text",
"created_at": "2024-01-01T12:00:00"
}
]
}{
"type": "text",
"content": "Hello, everyone!"
}{
"id": "550e8400-e29b-41d4-a716-446655440003",
"room_id": "550e8400-e29b-41d4-a716-446655440000",
"user_id": "550e8400-e29b-41d4-a716-446655440001",
"content": "Hello, everyone!",
"type": "text",
"created_at": "2024-01-01T12:00:00"
}The API implements rate limiting to prevent abuse:
- HTTP endpoints: 60 requests per minute per IP
- WebSocket connections: 1 connection per user per room
Messages are stored in Redis with the following characteristics:
- Messages expire after 7 days (configurable)
- Each room has a maximum of 100 messages (configurable)
- Room and user data are stored as Redis hashes
- Messages are stored as Redis lists with automatic trimming
The API uses standard HTTP status codes:
- 200: Success
- 400: Bad Request
- 404: Not Found
- 429: Too Many Requests
- 500: Internal Server Error
WebSocket errors are handled with close codes:
- 4004: Room or User Not Found
- 1011: Internal Server Error