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.1.tar.gz (17.1 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.1-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: socketspec-0.1.1.tar.gz
  • Upload date:
  • Size: 17.1 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.1.tar.gz
Algorithm Hash digest
SHA256 e5b590e2c5b8aef5183e23d548cd60eff92b4a4b647ec87905ce63d6c60b20de
MD5 68f9537e8c27c5ffb405c8db3688fe2a
BLAKE2b-256 e4949b9883c923faf41c9d9ea2d6cc5322cc2fd57647e4c812456049d816e0b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for socketspec-0.1.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: socketspec-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9406c2cbfa3ea175237c0dce3d8e6c321cfb8e8aa546aeb92e324c3b8a4b70d6
MD5 e2d76686065f26e355842a6e31c9808a
BLAKE2b-256 764023a3b33f6964283dde309098a1cc57a3d92839a29a7efe4f99552cb8fc1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for socketspec-0.1.1-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