- 最終更新
- 2026/10/21
3秒でわかる
データを全部そろえてから送らず、できた分から順に流す通信方式。待ち時間を短くし、巨大なデータも少ないメモリで扱うために使います。
30秒図解
もう少し詳しく
どういうものか
ストリーミングは、
なぜ必要か
理由は2つあります。1つは待ち時間です。LLMが2000文字の
具体例
// Server-Sent Events でサーバーから逐次送る
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
});
for await (const token of generate(prompt)) {
res.write(`data: ${JSON.stringify({ token })}\n\n`);
}
res.end();// 受け取り側は届いた断片をその場で描画する
const reader = (await fetch("/api/chat")).body
.pipeThrough(new TextDecoderStream())
.getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
output.textContent += value;
}つまずきやすいところ
途中に
似た用語との違い
| 方式 | 向き |
|---|---|
| ストリーミング | サーバーからの |
| WebSocket | 双方向に |
| ポーリング | クライアントが |


