Metadata-Version: 2.2
Name: fastapi-websocket-validation
Version: 0.1.1
Summary: A FastAPI WebSocket message validation and handling library
Home-page: https://github.com/zyx-db/fastapi-websocket-validation
Author: Dhiraj Bomma
Author-email: Dhiraj Bomma <dhirajbomma@gmail.com>
Project-URL: Homepage, https://github.com/zyx-db/fastapi-websocket-validation
Project-URL: Bug Tracker, https://github.com/zyx-db/fastapi-websocket-validation/issues
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.115.0
Requires-Dist: pydantic>=2.0.0
Dynamic: author
Dynamic: home-page
Dynamic: requires-python

# WebSocket Validation

A Python library for handling and validating WebSocket messages in FastAPI applications with Pydantic models.

## Installation

```bash
pip install fastapi-websocket-validation
```

## Features

- Type-safe WebSocket message handling
- Automatic payload validation using Pydantic models
- Schema generation for WebSocket messages
- Easy-to-use decorator-based message registration

## Quick Start

```python
from fastapi import FastAPI, WebSocket
from pydantic import BaseModel
from fastapi_websocket_validation import WebSocketHandler, WebSocketMessage

app = FastAPI()
ws_handler = WebSocketHandler()

# Define your message payload models
class ChatMessage(BaseModel):
    message: str
    room_id: str

# Register a handler for chat messages
@ws_handler.register_handler("chat", ChatMessage)
async def handle_chat(websocket: WebSocket, payload: ChatMessage):
    await websocket.send_json({
        "type": "chat",
        "payload": {"message": f"Received: {payload.message}"}
    })

# Register the outgoing chat messages
ws_handler.register_outgoing_message_schema("chat", ChatMessage)

# Handle incoming messages
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_json()
        message = WebSocketMessage(**data)
        await ws_handler.dispatch(websocket, message)

# Get the schema for all message types
@app.get("/schema")
async def get_schema():
    return ws_handler.get_schema()
```

## Documentation

For more examples and detailed documentation, visit our [GitHub repository](https://github.com/zyx-db/fastapi-websocket-validation).

## License

This project is licensed under the MIT License - see the LICENSE file for details. 
