D
Deno

help

forward stream response from openapi to frontend

AAbram3/5/2024
forward stream openapi by using deno backend as a middleware, how to receive and send stream to frontend ? using sse @Deno AI Helper
DAHDeno AI Helper3/5/2024
I'm sorry, but I wasn't able to find the answer to your question in the official Deno documentation. If you could provide more details or share a specific code snippet or error message, I might be able to spot any obvious syntax errors or mistakes. Please note that any advice I give is not based on the Deno documentation, and I recommend double-checking the accuracy of the advice given. If you have a specific question or issue, feel free to share it, and I'll do my best to assist you within my capabilities.
AAbram3/5/2024
@Deno AI Helper how can fresh post handlers return a stream api
DAHDeno AI Helper3/5/2024
As of my last update, the Fresh framework for Deno, which is a modern web framework, doesn't have explicit documentation on returning a Stream API directly from post handlers. However, in general, HTTP handlers in Deno can return a ReadableStream to the client. Here's a basic example of how you might return a stream from a handler in a Deno server context, but please note that this is not specific to Fresh and is not based on the Fresh documentation:
async function streamHandler(req: Request): Promise<Response> {
const stream = new ReadableStream({
start(controller) {
// You would typically generate your data here and enqueue it
controller.enqueue("data part 1");
controller.enqueue("data part 2");
controller.close();
}
});

return new Response(stream, {
headers: { "Content-Type": "text/plain" },
});
}
async function streamHandler(req: Request): Promise<Response> {
const stream = new ReadableStream({
start(controller) {
// You would typically generate your data here and enqueue it
controller.enqueue("data part 1");
controller.enqueue("data part 2");
controller.close();
}
});

return new Response(stream, {
headers: { "Content-Type": "text/plain" },
});
}
In the context of Fresh, you would need to adapt this to fit within the framework's handler structure. Since this advice is not based on the Deno documentation, please ensure to double-check the accuracy and consult the Fresh documentation or community for more specific guidance. Fresh might have its own way of handling streams that should be adhered to for best practices within the framework.

Looking for more? Join the community!

Recommended Posts