Skip to content

Streaming

Lanarky uses Server-sent events to implement streaming support over HTTP.

StreamingResponse

The StreamingResponse class follows the EventSource protocol to send events to the client.

Example

To understand how to use StreamingResponse, let's look at an example.

from lanarky import Lanarky
from lanarky.responses import StreamingResponse

app = Lanarky()


@app.get("/")
def index():
    def stream():
        for word in ["Hello", "World!"]:
            yield word

    return StreamingResponse(content=stream())

Here, we have a simple endpoint streams the message "Hello World!" to the client. StreamingResponse takes a generator function as its content, iterates over it and sends each item as an event to the client.

To receive the events, let's build a simple client script.

from lanarky.clients import StreamingClient


def main():
    client = StreamingClient()
    for event in client.stream_response("GET", "/"):
        print(f"{event.event}: {event.data}")


if __name__ == "__main__":
    main()

First run the application server.

uvicorn app:app

Then run the client script.

python client.pymessage: Hello
message: World!

Warning

The StreamingResponse classes inside the Adapters API behave differently from the above example. To learn more, see Adapters API documentation.