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 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.0.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.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cbd48c14a07ef21d8a82368a3b665dfcb1a41ec2a5c2efe36b5b0c36cd7d152f
MD5 4f744258f97d8a2e23947f3b61677283
BLAKE2b-256 a85c4cf6c9a8f3a8ea0d620be9b89a71421954cfd80a8c3c686f68b56f106170

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4102fd6e0a9174f95fb547960dd588046c5264f283b5932e1061cecdc8ce6f7b
MD5 4898d09c1f06b4f75be639914986b61e
BLAKE2b-256 0ac20d6a279e0014474638bca2536673c170025af5482d0ee3a8898027e1f322

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