Async & WebSockets

The same app runs under WSGI and ASGI. Write async handlers and WebSockets, and serve them with nothing extra installed.

Async handlers

@app.get("/feed")
async class=kw>def feed(req):
    data = await fetch_async()     class=com># await anything
    class=kw>return {"items": data}

Handlers may be async def (awaited) or plain (run directly) โ€” mix freely. Under ASGI, lifespan drives @app.on_startup / @app.on_shutdown.

WebSockets

@app.websocket("/ws/<room>")
async class=kw>def chat(ws):
    await ws.accept()
    async for msg in ws:                 class=com># loop until the client disconnects
        await ws.send("echo: " + msg)    class=com># or ws.send_json({...})

The WebSocket object exposes accept(), receive(), send()/send_json(), close(), and path params on ws.params.

Running async

app.run_async()                class=com># built-in zero-dep asyncio server (http + websockets)
class=com># or, in production, any ASGI server:
class=com>#   uvicorn mymodule:app

The built-in server speaks HTTP/1.1 and RFC 6455 WebSockets, so async mode needs no uvicorn. The app is also a standard ASGI callable for uvicorn/hypercorn/daphne. Guides: async framework ยท WebSockets.