How to simulate Open AI's word by word stream works in the backend?

I have a completed string and i want to deliver it word by word instead of dumping the whole string. Apparently the example from chatgpt doesnt works

import { type NextApiRequest, type NextApiResponse } from "next";
import stream from "stream";

export default function Handler(req: NextApiRequest, res: NextApiResponse) {
  // Set the response headers to indicate it's a text stream
  res.setHeader("Content-Type", "text/event-stream");

  // Create a readable stream from your string
  const wordsToStream =
    "This is the string you want to s as daslfalfj kldfklsad nhkgl skgskdg njkhdgnkjdnj njknjksjgnsjd gsdng ds sndgk dskl stream\n";

  const words = wordsToStream.split(" ");
  let currentIndex = 0;

  const readableStream = new stream.Readable({
    read(size) {
      if (currentIndex < words.length) {
        this.push(words[currentIndex] + " "); // Push a word with a space
        currentIndex++;
        setTimeout(() => {
          this._read();
        }, 1000); // Delay for 500 milliseconds
      } else {
        this.push(null); // Signal the end of the stream
      }
    },
  });

  // Pipe the readable stream to the response
  readableStream.pipe(res);
}
Was this page helpful?