Wait and Stream
View as MarkdownA 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.
@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
func (controller *controller) read(request *gin.Context) {
flowID, found := httputil.RequiredQuery(request, "workflowId")
if !found {
return
}
var value string
message, err := controller.client.ReadStream(
request.Request.Context(),
flowID,
Progress,
request.Query("resumeToken"),
&value,
)
if err != nil {
request.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
request.JSON(http.StatusOK, gin.H{
"value": value,
"resumeToken": message.ResumeToken,
"createdTime": message.CreatedTime.Format(time.RFC3339Nano),
"source": message.Source,
})
}
@GetMapping("/read")
public ResponseEntity<Map<String, String>> read(
@RequestParam final String workflowId,
@RequestParam(defaultValue = "") final String resumeToken) {
final StreamMessage<String> message = client.readStream(
workflowId,
flow.progress,
resumeToken,
Duration.ofSeconds(20));
final Map<String, String> response = new LinkedHashMap<String, String>();
response.put("value", message.getValue());
response.put("resumeToken", message.getResumeToken());
response.put("createdTime", message.getCreatedTime().toString());
response.put("source", message.getSource());
return ResponseEntity.ok(response);
}
Example: examples/java/src/main/java/io/superdurable/dex/primitives/stream/StreamController.java
router.get("/read", async (request, response) => {
const message = await client.readStream(
String(request.query.workflowId ?? ""),
progress,
String(request.query.resumeToken ?? ""),
20_000,
);
response.json(message);
});
Example: examples/typescript/src/primitives/stream/controller.ts
async fn read(
State(client): State<SharedClient>,
Query(query): Query<ReadQuery>,
) -> impl IntoResponse {
match run_blocking(move || {
client
.read_stream_with_timeout(
&query.workflow_id,
&PROGRESS,
&query.resume_token,
Duration::from_secs(20),
)
.map(|message| ReadResponse {
value: message.value,
resume_token: message.resume_token,
created_time: format!("{:?}", message.created_time),
source: message.source,
})
}) {
Ok(response) => ok_json(response),
Err(error) => map_sdk_error(error).into_response(),
}
}
Rules and pitfalls
- Wait for durable conditions another way. Stream messages are best effort and are not the Flow result. When the caller needs one durable condition before it proceeds, use Wait for Step Completion or Wait for Attribute Match.
The Polling with Long-Running Step Heartbeat pattern reports job progress on a Stream that clients read the same way.