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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3705c4114be42d46f26033a1f46811d2009c4efa85c2d34d357ffdcba7d33c79
MD5 81450160a2c85405afbed5f3dc2bdc0b
BLAKE2b-256 78c7e5824e186da9a89c18f35a5e124f7f116df8fe6735628679be1bc0f6c854

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 de80b09b1ade7b774af81650bab6dde7a46cc0ce513c141f6f93ea9efeeec7dd
MD5 cc4aba13493f0dac530b04e7bc5b25e6
BLAKE2b-256 ad8dc59eb8703e26761702a653e5b0ad7770f6b1540513cc0ff6d67ef1f76b8c

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