Skip to main content
The five data: lines arrive one second apart, as the app writes them; the edge forwards a response body as it is produced rather than waiting for the end.

Stream a response

An ASGI app streams whatever its framework streams: server-sent events, as above, chunked JSON lines, or a file. @app.endpoint is the wrong tool for this. It turns the function’s return value into one response, so a generator returned from an endpoint is serialized as a whole, not sent as it yields. timeout_seconds, 180 by default, bounds the wait until the response starts. Once the first bytes arrive the stream may run as long as the app keeps writing.

Send messages over a WebSocket

@app.realtime turns a function into a WebSocket handler. Each incoming message calls it, and the return value goes back as one message. A generator sends each item as its own message, so one request can report progress:
Strings go as text frames, bytes as binary frames, and anything else as JSON. An async generator works the same way. The handler may also take a context argument before the message. Realtime handlers take the ASGI options and deploy, preview, and scale the same way. A FastAPI or Starlette WebSocket route inside an ASGI app also works; the edge upgrades the connection and passes frames through in both directions.

Count the slots

A stream and a WebSocket connection each hold one of the container’s concurrent_requests slots until they end, and the autoscaler counts them as requests in flight. A chat service with a hundred open connections needs concurrent_requests and max_containers sized for a hundred, not for the message rate. See Scaling and concurrency.

Authenticate

Streams and WebSockets need the same Authorization: Bearer <token> header as any other request to the app, and authorized=False makes the URL public. A browser cannot set headers on an EventSource or WebSocket, so a page served by the same app usually mints a short-lived URL or token of its own; the image studio example signs its progress WebSocket URL this way.