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
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
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:
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.yeah I didn't mean to only return the stream once resolved
just a small project I can play around in
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