A FastAPI WebSocket message validation and handling library
Project description
WebSocket Validation
A Python library for handling and validating WebSocket messages in FastAPI applications with Pydantic models.
Installation
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
from fastapi import FastAPI, WebSocket
from pydantic import BaseModel
from 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.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_websocket_validation-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_websocket_validation-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cbd48c14a07ef21d8a82368a3b665dfcb1a41ec2a5c2efe36b5b0c36cd7d152f
|
|
| MD5 |
4f744258f97d8a2e23947f3b61677283
|
|
| BLAKE2b-256 |
a85c4cf6c9a8f3a8ea0d620be9b89a71421954cfd80a8c3c686f68b56f106170
|
File details
Details for the file fastapi_websocket_validation-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_websocket_validation-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4102fd6e0a9174f95fb547960dd588046c5264f283b5932e1061cecdc8ce6f7b
|
|
| MD5 |
4898d09c1f06b4f75be639914986b61e
|
|
| BLAKE2b-256 |
0ac20d6a279e0014474638bca2536673c170025af5482d0ee3a8898027e1f322
|