Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 305
Andrei Avatar asked Jan 06 '12 16:01

Andrei


People also ask

What is better than WebSocket?

SSE is best used when it's not necessary to send data from client to server. For example, in status updates and push notification applications, the data flow is from the server to the client only. This is what SSE is designed for, so WebSocket would be overkill.

How does server side event work?

SSE is a technology that provides asynchronous communication with event stream from server to the client over HTTP for web applications. The server can send un-directional messages/events to the client and can update the client asynchronously.

Is SSE faster than WebSockets?

SSEs are most appreciated in the cases like updating statuses, push notifications, newsletters and news feeds because they support mono-directional communication. Compared to WebSockets, SSE is faster and more suitable to set up.

What is the difference between server sent events SSEs and WebSockets in html5?

SSEs are sent over traditional HTTP. That means they do not require a special protocol or server implementation to get working. WebSockets on the other hand, require full-duplex connections and new Web Socket servers to handle the protocol.


1 Answers

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.

like image 165
kanaka Avatar answered Oct 12 '22 23:10

kanaka