Chapter 10 of 14
Streaming
A normal request waits for the complete response. A stream lets your program read small events while the service is still working.
Creole supports Server-Sent Events for live AI text, progress messages, and other event feeds.
Start a stream
Use the API connection from the previous chapter:
Stream payload to "/chat" using ai as events.events can be read with For each:
Show "AI: " without a new line.
For each event in events.
Show event's text without a new line.
End.
New line.The text appears piece by piece instead of waiting for the complete answer.
Event values
Each event can contain:
event— the event typedata— parsed event datatext— readable textid— the event identifierretry— a suggested reconnect delay
Read only the fields your service provides.
What Creole handles
Creole handles:
- event comments
- multiline event data
- JSON event data
- UTF-8 text
- blank event boundaries
Keep streamed output clean
Use without a new line for each text part, then add one final line:
For each event in events.
Show event's text without a new line.
End.
New line.Stream safely
- Set a request timeout and response limit.
- Do not assume every event contains text.
- Avoid printing private event data.
- Test interrupted connections.
- Keep the user informed while waiting.
Learning checklist
- I understand the difference between a request and a stream.
- I can start a stream.
- I can read every event.
- I can show text without extra lines.
- I know the common event fields.
Next step
Continue to Terminal and automation to receive command inputs and automate local work.