Skip to main content

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.

Metadata

Release files for socketspec 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for socketspec 0.1.2
File Size Uploaded Provenance Actions
socketspec-0.1.2.tar.gz 15.2 kB With provenance Details for socketspec-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for socketspec 0.1.2
File Interpreter ABI Platform Actions
socketspec-0.1.2-py3-none-any.whl Python 3 none any Details for socketspec-0.1.2-py3-none-any.whl

Total release size: 31.5 kB

Release files / socketspec-0.1.2.tar.gz

Download URL socketspec-0.1.2.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9451baacaf71c8982229f164863848404bddf63bb88917f6fbc6d51fd077aa4c
BLAKE2b-256 checksum
How to use checksums
f17b55871ad4baa631804ddd97815fb1e21cd07e0b4ffe1dfa8b1b13a83e54d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 27, 2026.

Transparency log

Release files / socketspec-0.1.2-py3-none-any.whl

Download URL socketspec-0.1.2-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2c6ca61b47be822caef087aea23e10170f50a25ebc19dd9dfd062288b917061
BLAKE2b-256 checksum
How to use checksums
8ce0f8b0a51b0ba0b113597ba9c1236beff11bccbebcc5ef3a155a0c1a4b2bf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 27, 2026.

Transparency log

Security for socketspec 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for socketspec-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 27, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page