CloudCode blocking OpenAI API streaming

@leebert Lee Brenner
Mon, 05 Jan 2026 23:04:18 +0000

Hello. I’m using Back4App to proxy OpenAI API responses to a client app and I’ve got everything working except for streaming. On the server side I am saving the streamed responses to an OpenAIResponse object and on the client side I use LiveQuery to listen for updates to that class so that I can display the response progressively to the user. However, when I call the CloudCode it appears to block everything and the LiveQuery events don’t fire until the function completes. Any ideas on what I’m doing wrong or how I might do this differently?

CloudCode

Parse.Cloud.define("testOpenAI", async (request) => {
  const OpenAIResponse = Parse.Object.extend("OpenAIResponse");
  const openAIResponse = new OpenAIResponse();
  openAIResponse.set('response', '');
  await openAIResponse.save();

  const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  });

  var tokenCount = 0;
  const stream = openai.responses
    .stream({
    model: 'gpt-5-nano-2025-08-07',
    instructions: 'You are a storyteller that talks like a pirate.',
    input: 'Tell a ten word story about a tardigrade.',
    stream: true,
    })
    .on("response.output_text.delta", (event) => {
      openAIResponse.set('response', `${openAIResponse.get('response')}${event.delta}`);
      tokenCount++;
      openAIResponse.set('tokenCount', tokenCount);
      openAIResponse.save();
    })
    .on("response.error", (event) => {
      console.error(event.error);
    });

});

Relevant Client Code

liveQueryClient.open();
const query = new Parse.Query("OpenAIResponse");
const subscription = liveQueryClient.subscribe(query);
subscription.on("update", data => {
  res.innerHTML = data.get('response');
});

async function testApp() {
  try {
    await Parse.Cloud.run("testOpenAI");
  } catch (e) {
    console.log(`testOpenAI failed - ${e}`);
  }
};

testApp().then(()=>{
  console.log('testApp completed')
});