Why is EventSource connection closed every 30-60 sec when no data transported, while WebSocket's one is kept open?
Asked Answered
G

2

16

I would like to push data to users every 2 min. Using EventSource requires additional pushing null-byte every 29 sec to keep the connection open. WebSocket doesn't require such ping. Why is the EventSource connection regularly closed and reopened? Is it because there is no good built-in way in HTTP to check if the connection is still open or other reason?

Graticule answered 6/1, 2012 at 16:27 Comment(0)
B
13

The Server-Sent Events (Eventsource) API is layered on HTTP. WebSocket is layered on TCP (but has an HTTP compatible handshake). Both HTTP and TCP often have idle timeouts however, the TCP timeouts tend to be much longer (e.g. 2 hours rather than 2 minutes). So you still might need keep-alive messages in WebSockets, but they could probably be much less frequent. Also, the WebSocket standard defines ping/pong frames that the browser/server may implement to do this for you.

Bauer answered 6/1, 2012 at 16:51 Comment(1)
There is nothing as TCP idle timeout. Some firewalls might cut idle connections unless TCP is configured to send keepalive packets (which it often is), but TCP itself never closes an idle conneciton. HTTP servers generally close the HTTP connection in order to save resources.Wootan
I
9

it may depend on your server-side software

node.js has 2 minutes default timeout

here is article about it - http://contourline.wordpress.com/2011/03/30/preventing-server-timeout-in-node-js/

solution:

res.connection.setTimeout(0); // this could take a while

Irreproachable answered 28/4, 2012 at 5:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.