node.js - Heroku H12 Request Timeout for Server Sent Events (SSE) Route
问题描述
I have a NodeJS app that uses a Server Sent Events (SSE) route to send updates from the server to the client. On my local dev environment, this works great since the client remains connected to the SSE route at all times and tries to reconnect immediately if it is disconnected.
However, once I deployed my app to Heroku, it all went awry. Within a few seconds of not sending any data over the SSE route, I get a 503 Service Unavailable
error on the client side and the client loses its connection to server due to which it is unable to receive any more real time updates. Looking at the Heroku server logs, it was giving me a H12 Request Timeout
error.
On some further research, I came across this article on the Heroku website:
If you’re sending a streaming response, such as with server-sent events, you’ll need to detect when the client has hung up, and make sure your app server closes the connection promptly. If the server keeps the connection open for 55 seconds without sending any data, you’ll see a request timeout.
However, it does not mention how to solve the issue.
- Is there a way to set the timeout to infinity?
- Or does this mean I have to keep sending heartbeats from my server to client just to keep the SSE route connection alive? This seems tedious and unnecessary since I want to keep the connection alive at all times.
解决方案
Received this from Heroku:
I wish I had better news for you but unfortunately, there's nothing you can do to avoid this other than sending a ping within every 55 seconds to keep the SSE persistent.
Heartbeat is the only way to keep an SSE route alive with Heroku unfortunately.
推荐阅读
- node.js - Lambda - 通过 Lambda 查询 DynamoDB
- html - bundle.js 未在静态 html 文件中成功加载,但请参阅
- database - 查询结果的小数位在 Apache IoTDB 中自动四舍五入
- javascript - 需要显示来自的短语
- python - 我正在使用基于类的 tkinter 来更改框架并现在导航我如何在这些类之间交换数据
- selenium - 如何找到以下 HTML 的元素?
- java - Java 8 流字符串操作和比较
- java - Spring cloud gateway 2.5 with eureka return 404
- react-native - expo 应用程序在网络上异常冻结,没有错误消息
- reactjs - 如何使用 VSC 在现有的 react 项目上安装 react 但安装了新的操作系统?