05 · streaming
Temps réel : SSE et WebSocket
Affichez la réponse au fil de l'eau au lieu d'attendre la fin du tour d'agent.
- POST chat/stream : mêmes paramètres que /chat, réponse text/event-stream.
- Événements : session (sessionId), token (texte partiel), final (réponse complète), error.
- WebSocket : namespace /re-act pour le temps réel bidirectionnel.
Streaming — JavaScript (fetch + SSE)
const res = await fetch("https://api.votredomaine.com/api/v1/re-act-agent/chat/stream", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ botId: "VOTRE_BOT_ID", sessionId, msisdn: "0700000000", message }),
});
const reader = res.body.getReader();
const dec = new TextDecoder();
let buf = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buf += dec.decode(value, { stream: true });
const parts = buf.split("\n\n");
buf = parts.pop() ?? "";
for (const evt of parts) {
if (!evt.startsWith("data: ")) continue;
const msg = JSON.parse(evt.slice(6));
if (msg.type === "token") append(msg.delta); // texte partiel
if (msg.type === "final") finish(msg.content); // réponse complète
if (msg.type === "error") fail(msg.message);
}
}