H
Hono2mo ago
Chris

Hono SSE Stream Closing Unexpectedly After 10 Seconds

Title: Hono SSE Stream Closing After 10 Seconds Issue: Using Hono's streamSSE, the SSE connection closes after ~10 seconds, regardless of client (curl/browser). A minimal Node.js SSE server works fine. Environment: Debian, localhost Code snippet:
app.get("/render", (c: Context) => {
return streamSSE(c, async (stream) => {
while (true) {
await stream.writeSSE({
data: `It is ${new Date().toISOString()}`,
event: "time-update",
});
await stream.sleep(1000);
}
});
});
app.get("/render", (c: Context) => {
return streamSSE(c, async (stream) => {
while (true) {
await stream.writeSSE({
data: `It is ${new Date().toISOString()}`,
event: "time-update",
});
await stream.sleep(1000);
}
});
});
Logs show:
SSE connection initiated
Sending message #1: It is 2024-08-30T14:21:10.214Z
...
Sending message #9: It is 2024-08-30T14:21:18.236Z
Stream aborted by client
SSE connection initiated
Sending message #1: It is 2024-08-30T14:21:10.214Z
...
Sending message #9: It is 2024-08-30T14:21:18.236Z
Stream aborted by client
Any ideas why Hono's SSE implementation might be closing the connection prematurely? Tried error handling and abort detection, issue persists. Thanks for any help!
12 Replies
Aditya Mathur
Aditya Mathur2mo ago
Which runtime are you using? I tried with Nodejs, seems to be working for me
Chris
Chris2mo ago
I am using bun as runtime Do you think this could be an issue?
Aditya Mathur
Aditya Mathur2mo ago
Maybe, try changing the runtime to nodejs for checking this 🧪 , and if the problem is resolved, try searching the bun docs or ask a question regarding this on their discord
ex0ns
ex0ns2mo ago
https://discord.com/channels/1011308539819597844/1011308539819597847/1277621456586670203 I had this issue since version 1.1.26 of bun.... this is a nasty setting to change in a patch
Chris
Chris2mo ago
Thank you so much! You saved us a lot of time. How did you solve this issue or better what do you reccomend to to? Should we just downgrade our version and wait till hono fixes their serve or is that something we need to configure by our own?
ex0ns
ex0ns2mo ago
I increased the value of idleTimeout
export default {
port: process.env.PORT ?? 8787,
fetch: app.fetch,
idleTimeout: 0,
};
export default {
port: process.env.PORT ?? 8787,
fetch: app.fetch,
idleTimeout: 0,
};
Chris
Chris2mo ago
Is setting the idleTimeout: 0, the same as setting it to inf? Or would you reccomend using a real value and actually making use of the feature
ex0ns
ex0ns2mo ago
Yes it is. But I only use bun to dev, in prod we use node.js so this value is only use locally, but I agree that you should probably set it to another value if you use bun in production
Chris
Chris2mo ago
Are there any reasons for your prof/dev setup? Did you choose node.js for prod because of stability? We're running bun in both dev and prod but often debate if node is a more mature and stable option. So far bun did a good job but more recently we are facing more and more issues
ex0ns
ex0ns2mo ago
Honestly, I just node.js because I'm more familiar with it and was easier for me to work with Node.js, however due to some monorepo, bundling and typescript setup running our server locally on our laptop became a pain in the a** (tsx, ts-run, you name it always failed at some point), I decided to try bun to run TS natively and it worked amazingly so far.
BridgeSense
BridgeSense2mo ago
thanks so much for this, just spent a few hours trying to fix it
IvanCN
IvanCN2mo ago
Fixed: Default idleTimeout no longer set in node:http In Bun 1.1.26, the default idleTimeout was 10 seconds, which was a breaking change as versions before had unlimited timeout. The default value of idleTimeout is now 0, which disables the timeout.
https://bun.sh/blog/bun-v1.1.27#fixed-default-idletimeout-no-longer-set-in-node-http
Bun
Bun v1.1.27
Fixes 130 bugs (addressing 250 👍). bun pm pack, faster node:zlib. Static routes in Bun.serve(). ReadableStream support in response.clone() & request.clone(). Per-request timeouts. Cancel method in ReadableStream is called. bun run handles CTRL + C better. Faster buffered streams. bun outdated package filtering. Watch arbtirary file types...
Want results from more Discord servers?
Add your server