Server-Sent Events connection timeout on Node.js via Nginx Server-Sent Events connection timeout on Node.js via Nginx nginx nginx

Server-Sent Events connection timeout on Node.js via Nginx


Answering to myself. Actually solution was not that difficult to find, it just demanded careful look into nginx documentation.

proxy_read_timeout is a directive responsible for that, and by default it's set to 60 seconds. So it can be easily fixed by setting e.g.:

proxy_read_timeout 24h;

Setting 0 won't work, it will actually make all your connections broken, therefore we need to come up with long enough timeout.

After fixing that I approached also the other issue, but this time related to how browsers handle the connection. For some reason after 5 minutes of inactivity browsers silently discard the connection. What's worse neither side is informed that it's discarded, for both it still appears as if connection is online, but data doesn't get through. Fix for that is to send some keep alive ping on interval basis (plain sse comment works great).