Skip to main content

FastAPI-style WebSocket framework with built-in docs and testing

Project description

SocketSpec

FastAPI-style WebSocket framework with built-in interactive docs and testing.

CI PyPI Python Coverage License


Install

pip install socketspec[fastapi]

Quickstart

from fastapi import FastAPI
from pydantic import BaseModel
from socketspec import SocketApp
from socketspec.adapters.fastapi import mount

socket = SocketApp(docs=True)

class ChatMessage(BaseModel):
    room: str
    text: str

class MessageAck(BaseModel):
    status: str
    message_id: str

@socket.on(
    "send_message",
    description="Send a chat message to a room.",
    tags=["chat"],
    emits=[Emits("message_ack", model=MessageAck, description="Delivery confirmation")],
    broadcasts=[Broadcasts("new_message", room="chat:{room}", description="Delivered to room members")],
)
async def send_message(conn, payload: ChatMessage) -> None:
    await conn.emit("message_ack", {"status": "ok", "message_id": "abc123"})
    await socket.rooms.broadcast(
        "chat:" + payload.room,
        "new_message",
        {"from": conn.id, "text": payload.text},
    )

app = FastAPI()
mount(socket, app, path="/ws")
uvicorn main:app --reload

Docs UI

Open /socket-docs in your browser after starting the server.

  • Click an event card to expand its schema
  • Hit Try it out to send a live WebSocket message
  • See the server response appear inline, without leaving the browser
  • Open a second tab and connect as a different user to test broadcasts

Why SocketSpec

Feature python-socketio channels (Django) SocketSpec
FastAPI-native
Pydantic payload validation
Built-in interactive docs
TestClient for unit tests partial
Room guards / permissions manual manual
Dependency injection (Depends)
Type-safe (mypy --strict)

Core Concepts

Event handlers look exactly like FastAPI route handlers:

@socket.on("join_room", tags=["rooms"])
async def join_room(conn: Connection, payload: JoinPayload) -> None:
    await socket.rooms.join(conn, f"chat:{payload.room_id}")

Rooms with pattern-based guards:

@socket.room_guard("admin:{room}")
async def admin_only(conn: Connection, room: str) -> bool:
    return conn.identity.role == "admin"

Testing without a real server:

from socketspec.testing import TestClient

async def test_send_message():
    async with TestClient(socket) as client:
        conn = await client.connect()
        await conn.send("send_message", {"room": "general", "text": "hello"})
        response = await conn.receive()
        assert response["event"] == "message_ack"

Links


License

Apache 2.0 — see LICENSE. Created and maintained by Laiba Shahab.

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

socketspec-0.1.2.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

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

socketspec-0.1.2-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: socketspec-0.1.2.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for socketspec-0.1.2.tar.gz
Algorithm Hash digest
SHA256 9451baacaf71c8982229f164863848404bddf63bb88917f6fbc6d51fd077aa4c
MD5 dc449d7fbc0b32a1d8017e683659cff9
BLAKE2b-256 f17b55871ad4baa631804ddd97815fb1e21cd07e0b4ffe1dfa8b1b13a83e54d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for socketspec-0.1.2.tar.gz:

Publisher: publish.yml on ByteCraftByLaiba/socketspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: socketspec-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for socketspec-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c2c6ca61b47be822caef087aea23e10170f50a25ebc19dd9dfd062288b917061
MD5 bf80bd72f8281d165aabad3420340c2e
BLAKE2b-256 8ce0f8b0a51b0ba0b113597ba9c1236beff11bccbebcc5ef3a155a0c1a4b2bf5

See more details on using hashes here.

Provenance

The following attestation bundles were made for socketspec-0.1.2-py3-none-any.whl:

Publisher: publish.yml on ByteCraftByLaiba/socketspec

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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