05 · streaming
Realtime: SSE and WebSocket
Render the answer as it streams instead of waiting for the agent turn to finish.
- POST chat/stream: same parameters as /chat, text/event-stream response.
- Events: session (sessionId), token (partial text), final (complete answer), error.
- WebSocket: /re-act namespace for bidirectional realtime.
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);
}
}