Ushka

ushka.http.stream

Streaming responses and Server-Sent Events (SSE).

StreamingResponse sends chunks as an async (or sync) iterator produces them, and stops the iterator when the client goes away. EventSourceResponse speaks SSE on top of it:

async def get(request):
    async def events():
        for n in range(5):
            yield {"count": n}              # a dict is sent as JSON
            await asyncio.sleep(1)
    return EventSourceResponse(events())

In this module

class StreamingResponseA response whose body is produced chunk by chunk.
class ServerSentEventOne SSE message. data may be text (multi-line is fine) or a dict/list sent as JSON.
class EventSourceResponseServer-Sent Events. events yields ServerSentEvent, text, or dict/list (JSON).

class StreamingResponse (Response)

A response whose body is produced chunk by chunk.

content is an async or sync iterable of bytes or str. Cookies and the session are saved with the headers, so set them before the first chunk is produced (they are, when you return the response from the handler). Cleanup in the iterator (finally) runs when the client disconnects.

class ServerSentEvent

One SSE message. data may be text (multi-line is fine) or a dict/list sent as JSON.

encode

def encode() -> bytes

class EventSourceResponse (StreamingResponse)

Server-Sent Events. events yields ServerSentEvent, text, or dict/list (JSON).

A comment line is sent every ping seconds (None to disable) so proxies keep the connection open.