How to send back multiple partial responses for an HTTP Post request?

148 Views Asked by At

I am implementing a Supabase Edge function, which runs on Deno runtime, and uses the Oak framework. During the processing of my HTTP POST request, I want to send back partial responses to the client. Is this possible with Oak? If not, is there any other middleware I could use?

1

There are 1 best solutions below

0
cooow On

Yes, it is perfectly possible to send partial responses (i.e. chunked responses) using the Oak framework in Deno. Oak supports the sending of chunked responses using the response.write() method.

This allows you to send chunks of data to the client as they become available or are generated, rather than waiting until the end of processing to send the full response.

router.post("/yourRoute", async (ctx) => {
  ctx.response.type = "text/plain";
  ctx.response.status = 200;

  await ctx.response.write("first data part\n");

  for (let i = 1; i <= 5; i++) {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    await ctx.response.write(`Data chunk ${i}\n`);
  }

  ctx.response.end();
});