Skip to main content

Wait and Stream

View as Markdown

A caller reads a Flow's Stream one message at a time and resumes from the token of the last one. Use it when the caller shows a series of updates, such as progress, instead of waiting for one point.

How it works

The Flow writes messages to a Stream. ReadStream waits for the next message after the supplied resume token. The response includes a new token. Pass it to the next read to continue in order.

In the example, StreamFlow writes to a progress Stream. Its read endpoint long-polls for the next message, returns one message, and gives the client the token to keep. The client can issue the next read right after it handles that message.

Core implementation

The read endpoint takes the caller's token and returns the message with a new resume token. The Go example uses the server's wait cap; the others request up to 20 seconds.

controller.pyPython
@blueprint.get("/read")
async def read() -> Response:
message = await app_state.client.read_stream(
required_query("workflowId"),
app_state.stream.progress,
optional_query("resumeToken", ""),
timedelta(seconds=20),
)
return jsonify(
value=message.value,
resume_token=message.resume_token,
created_time=message.created_time.isoformat(),
source=message.source,
)

Example: examples/python/dex_examples/primitives/stream/controller.py

Rules and pitfalls

The Polling with Long-Running Step Heartbeat pattern reports job progress on a Stream that clients read the same way.

Next