H
Hono4d ago
reko

How to wait for streamSse to finish?

I am using hono's streamSSE helper to stream responses, which works great. However it seems that the streamSSE returns right away, and I have no way to await when the streaming is done. I have a middleware that collects metrics such as http request timings, and the middleware also returns immediately from await next() instead of waiting the streaming to be done, so the timings are completely wrong. Is there any way, in the middleware, to wait for the streaming to be done?
4 Replies
Arjix
Arjix4d ago
you could probably use a promise that resolves when the stream is done can you write a small repro? nothing much, just the middleware, and the Hono route using streamSSE that way I can give an example of what I meant
reko
rekoOP4d ago
Let me try that, my only worry is that will it somehow mess up the streaming if I don't return the Response object immediately. Yeah so I tried this:
const response = await new Promise((resolve) => {
const response = streamSSE(c, async (stream) => {
// do all the streaming etc.
resolve(response);
})
});
return response;
const response = await new Promise((resolve) => {
const response = streamSSE(c, async (stream) => {
// do all the streaming etc.
resolve(response);
})
});
return response;
But then it doesn't stream at all as I was afraid. My guess is that since Hono doesn't get the response object back from the handler, it won't process the stream. I guess what I can do as a work-around is use deferred promise pattern and put a promise in the request context and then await for that to finish in the middleware. But I was hoping my middleware wouldn't have to care abou whether its dealing with streaming or regular requessts I can create a small repro anyways if you want to fiddle around.
Arjix
Arjix4d ago
yeah I didn't mean to only return the stream once resolved just a small project I can play around in
ambergristle
ambergristle3d ago
this might be helpful: https://discord.com/channels/1011308539819597844/1346883724825854023 streamSSE is also only ~30 lines. you could copy+paste and modify locally to set a flag or whatever

Did you find this page helpful?