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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With