首页 > 解决方案 > 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.

  1. Is there a way to set the timeout to infinity?
  2. 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.

标签: node.jsherokuserver-sent-events

解决方案


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.


推荐阅读