Skip to main content

ASGI Hosting

SignalRServer.asgi_app() returns a standard ASGI application that serves the negotiate / WebSocket / SSE / Long Polling / POST endpoints for your hub. Mount it inside uvicorn, FastAPI or any ASGI server.

Standalone uvicorn

import uvicorn

from aiosignalr.server import Hub, SignalRServer


class ChatHub(Hub):
async def echo(self, text: str) -> str:
return text


server = SignalRServer(ChatHub)
app = server.asgi_app(path="/hub")

if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)

Inside FastAPI

from fastapi import FastAPI

from aiosignalr.server import Hub, SignalRServer


class ChatHub(Hub):
async def echo(self, text: str) -> str:
return text


app = FastAPI()
server = SignalRServer(ChatHub)
app.mount("/hub", server.asgi_app(path="/hub"))

The hub now lives at http://host:8000/hub.

Mounting in an existing ASGI tree

Because asgi_app() is itself a plain ASGI app, you can compose it with any routing layer. The endpoint path is fully controlled by path.

Requirements

  • The ASGI server must support WebSocket upgrade (uvicorn does; some production proxies need WebSocket support enabled).
  • For stateful reconnect, the underlying ASGI server must keep the connection open and allow reuse of the same connection token across reconnects (uvicorn handles this).

Note on lifecycle

The returned app is stateless and process-local. If you need to share hub state across workers or machines, plug a custom lifetime manager — see HubLifetimeManager.