Comparing WebSockets and Server-Sent Events
This article compares two popular choices for realtime applications - WebSockets and Server-Sent Event (SSE) APIs. WebSockets provide a full-duplex, low-latency, event-driven connection between the server and browser, ideal for realtime apps. They use a custom ws protocol to transport messages at a lower level than HTTP and are useful for applications that require data to be read from and written to the server such as chat apps or multiplayer games. WebSockets have bi-directional communication in realtime but may face issues with some enterprise firewalls and lack built-in support for reconnection. Server-Sent Events (SSEs) are based on Server-Sent DOM Events, allowing browsers to subscribe to a stream of events generated by a server using the EventSource interface. They use XHR streaming to transport messages over HTTP and are useful for apps that only require reading data from the server such as live stock or news tickers. SSEs have built-in support for reconnection, no issues with enterprise firewalls, but can't handle binary data and have a limit of six concurrent open connections per browser. The choice between WebSockets and Server-Sent Events depends on the use case. WebSockets are more complex and demanding but provide a full-duplex TCP connection useful for multiplayer games, chat apps, or location-based apps. SSEs are better for simple cases where you don't need two-way messaging such as read-only realtime apps like stock tickers or news updates.
Company
Ably
Date published
Sept. 30, 2019
Author(s)
Eve Martin
Word count
1988
Hacker News points
None found.
Language
English