Discussions

Ask a Question
Back to All

Wrong order of chunks read - Streaming API

Hi,

I'm experiencing an issue with the Streaming API. When sending chunks from the backend and logging them in the console on the frontend, I can see that each chunk is logged in the correct order. However, the avatar reads the messages in a different, seemingly random order.

I've tried making some adjustments, but I haven't been able to fix the issue. Could you help me understand what might be causing this and how to resolve it?

Thanks for your help!


async function handleSpeak() {  
  if (userInput.value) {  
    start_time = Date.now()  
    const ws = new WebSocket("ws://localhost:8888");// Replace with your WebSocket backend URL
ws.onopen = () => {
  console.log("WebSocket connection established");
  ws.send(
    JSON.stringify({
      user_input: userInput.value, // Send user input to the backend
    })
  );
};

 ws.onmessage = async (event) => {
  try {
    const data = JSON.parse(event.data);
    if (data.chunk && avatar != null) {
      console.log("chunk: ", data.chunk)
       await avatar.speak({
        text: data.chunk,
        taskMode: TaskMode.ASYNC,
        taskType: TaskType.REPEAT,
      });
    }

    if (data.complete) {
      console.log("Streaming complete");
      ws.close();
    }
  } catch (error) {
    console.error("Error processing WebSocket message:", error);
  }
};