Skip to main content

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 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.

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

fastapi_websocket_validation-0.1.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_websocket_validation-0.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_websocket_validation-0.1.2.tar.gz.

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6b1bef6c083dd6396dbf2458bda822a3a0a17cceb5017be922f0ead6d267c1fa
MD5 2f6620f7fa687c96ed683e25c7d84b55
BLAKE2b-256 5e164fa5136881c65b82a1e1c6df8f82c359f266cf139c00dcb835ba85c323ab

See more details on using hashes here.

File details

Details for the file fastapi_websocket_validation-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 960e1b882ecad501ab825e8448f648c37c125934e0fb35b3f06870f35f2ede43
MD5 e2cf91076c272c17fc0177cc50a5eaac
BLAKE2b-256 ab9846f0b0ea2e9c44cc1f2ec33e9a73eeb4d0c1d995601e8e0b7befb9a54bf4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page