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.3.tar.gz (4.5 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.3-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.3.tar.gz
Algorithm Hash digest
SHA256 fe4a984bd5c867c99016d203392a10a594d381514933749c8858ee6fb6df1e12
MD5 fd183d72c784314b8b63a27554f51701
BLAKE2b-256 79a1202a2d9682dbc7b502e98a824c0ba916c946b5c63402edd84af102f6d11f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_websocket_validation-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 39ab9937fc4554768250408d38d0837666e0517aa437052ce22f8affdaa963e0
MD5 08d0da03b41fa1add54ee2d085634fd2
BLAKE2b-256 7856afbd80fc76bfc92db682b30c3374940e928e1f975312e8ee57b3158115b6

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