How can I flush a stream to a response?
I'm trying to stream a response but it appears that data is only sent after either a newline is written or some buffer is filled. Is there a way to manually flush the buffer?
To repro, run this with
--allow-net
, then observe the difference in behavior between curl http://127.0.0.1:8000/%0a
and curl http://127.0.0.1:8000/%20
.
4 Replies
i'm trying to render html, considering maybe just inserting comments like to make it flush earlier
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the code snippet you've provided, there doesn't seem to be any obvious syntax errors or mistakes.
In Deno, as in many server-side JavaScript environments, the underlying system may buffer output streams for efficiency. The behavior you're observing, where data is sent after a newline or when a buffer is filled, is typical of such systems. Unfortunately, without explicit support in the Deno standard library for flushing a stream, there's no guaranteed way to manually flush the buffer.
The code you've shared sets up a server that streams responses to the client. If Deno's standard library does not provide a method to flush the stream, this behavior might be inherent to the way the HTTP server is implemented or how the underlying TCP/IP stack is buffering the data.
Please note that this advice is not based on the Deno documentation, and I would recommend double-checking the accuracy of this advice and exploring the Deno community resources or forums for more insights into stream flushing.
is there are full working example? cant get my deno to flush 😳
nvm had to add { headers: { "content-type": "text/html; charset=utf-8" } }
and now it works
Thanks! This worked for me too!
no more silly hacks!