Skip to main content

Canonical ASGI adapter for PondSocket.

Project description

pondsocket-asgi

Canonical ASGI adapter for pondsocket. Mount under any ASGI host — FastAPI, Starlette, Litestar, Django, raw uvicorn.

Handles WebSocket and SSE (Server-Sent Events) transports. The same PondSocket instance serves both — the adapter detects the request type and dispatches.

Install

pip install pondsocket-asgi

Pulls in pondsocket and pondsocket-common automatically.

Quick start: raw uvicorn

from pondsocket import ConnectionContext, EventContext, JoinContext, PondSocket
from pondsocket_asgi import PondASGIApp


async def auth(ctx: ConnectionContext) -> None:
    ctx.accept()


async def on_join(ctx: JoinContext) -> None:
    await ctx.accept()


async def on_message(ctx: EventContext) -> None:
    await ctx.broadcast(ctx.event_name, ctx.get_payload())


pond = PondSocket()
endpoint = pond.create_endpoint("/socket", auth)
lobby = endpoint.create_channel("/chat/:room_id", on_join)
lobby.on_message("message", on_message)

app = PondASGIApp(pond)
uvicorn module:app --host 127.0.0.1 --port 8000

WebSocket clients connect to ws://127.0.0.1:8000/socket.

Mount under FastAPI

from fastapi import FastAPI
from pondsocket_asgi import PondASGIApp

app = FastAPI()
app.mount("/ws", PondASGIApp(pond))

When mounted at /ws, the adapter sees scope["path"] already stripped of the prefix — register your pond endpoints with the post-mount path (e.g. /socket resolves at ws://host/ws/socket).

See examples/ for FastAPI, Starlette, and raw uvicorn variants.

SSE transport

The same endpoint serves Server-Sent Events to clients that don't have WebSocket:

GET /socket
  Accept: text/event-stream
→ 200, response carries X-Connection-ID header and a `CONNECTION` event with `payload.connectionId`, stream of "event: <name>\ndata: <json>\n\n" frames

POST /socket
  X-Connection-ID: <id>
  Content-Type: application/json
  body: <client message JSON>
→ 204

DELETE /socket
  X-Connection-ID: <id>
→ 204

Use SSE when WebSocket is blocked (some corporate proxies). The connection handler is the same — the adapter routes by Accept header and method.

Lifespan

PondASGIApp handles the ASGI lifespan protocol. On lifespan.startup, it acknowledges immediately. On lifespan.shutdown, it closes the PondSocket (which closes every endpoint, every channel, every transport).

Testing

pondsocket_asgi.testing exposes an in-memory ASGI driver — no real network needed:

from pondsocket_asgi.testing import ASGITestDriver

driver = ASGITestDriver(PondASGIApp(pond))

# WebSocket
session = await driver.connect_websocket("/socket")
await session.send_client_message(
    action="JOIN_CHANNEL",
    channel_name="/chat/1",
    event="join",
)
ack = await session.receive_json()
await session.disconnect()

# SSE
sse = await driver.connect_sse("/socket")
status, _ = await driver.sse_push(sse.connection_id, {"action": "BROADCAST", ...})
frame = await sse.receive_event()
await sse.disconnect()

# Lifespan
lifespan = await driver.open_lifespan()
await lifespan.startup()
await lifespan.shutdown()

Public API

from pondsocket_asgi import ...

  • PondASGIApp — the adapter itself
  • ASGIWebSocketTransport — WebSocket transport (you usually don't construct this directly; the app does)

from pondsocket_asgi.testing import ...

  • ASGITestDriver, WebSocketSession, LifespanSession, SSESession

License

GPL-3.0-or-later.

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

pondsocket_asgi-0.0.5.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

pondsocket_asgi-0.0.5-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file pondsocket_asgi-0.0.5.tar.gz.

File metadata

  • Download URL: pondsocket_asgi-0.0.5.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for pondsocket_asgi-0.0.5.tar.gz
Algorithm Hash digest
SHA256 5c5daaa3482ae161f1ca3b048752f33739b4ac9bc7a1d187a7dc83fe67b0e71b
MD5 245673a38932945085c62575aabbdc0c
BLAKE2b-256 1b64c7a5467a711ae749b2a1f71961ed03648c59e57ca4e47c7f8c95f56086f4

See more details on using hashes here.

File details

Details for the file pondsocket_asgi-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for pondsocket_asgi-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3bc2c99059f108db566b69551124345a46c1646cffea87e77c7f2a9ec085e1b1
MD5 f695d376b5c6ea7bcd71189472770ed2
BLAKE2b-256 59db87fb5c76f92a126112540b71a6eec42d5a458f41b21f8a3124a3ca69ea3b

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