How do I stream tokens from an API into this UI as they arrive?
V
Great question. The cleanest way is to read the response as a stream and append each chunk to the message as it arrives — that's exactly what this kit's renderer does. Two steps:
- Fetch with a streaming-capable endpoint and read the
ReadableStream. - Decode each chunk and call the kit's
appendToken()helper.
javascript
const res = await fetch("/api/chat", { method:"POST" }); const reader = res.body.getReader(); const dec = new TextDecoder(); while (true) { const { value, done } = await reader.read(); if (done) break; appendToken(dec.decode(value)); // → renders live }
Try it yourself — send a message below and watch it stream in. 👇
0
Enter to send · Shift+Enter for new line · this is a front-end kit — wire it to any model