The blog post "You might not need WebSockets" argues that developers often prematurely choose WebSockets for real-time features when simpler, more efficient solutions exist. It highlights server-sent events (SSE) as a robust alternative for unidirectional communication from server to client, offering benefits like automatic reconnection and built-in event handling. While acknowledging WebSockets' bi-directional capabilities, the post emphasizes that many use cases only require server-to-client updates, making SSE a lighter and potentially better-performing choice. It encourages developers to carefully analyze their needs before defaulting to WebSockets and consider the reduced complexity and improved resource utilization that SSE can provide.
This post explores architectural patterns for adding realtime functionality to web applications. It covers techniques ranging from simple polling and long-polling to more sophisticated approaches like Server-Sent Events (SSE) and WebSockets. The author emphasizes choosing the right tool for the job based on factors like data volume, connection latency, and server resource constraints. They also discuss the importance of considering connection management, message ordering, and error handling. The post provides practical advice and code examples using JavaScript and Node.js to illustrate the different patterns, highlighting their strengths and weaknesses. Ultimately, it aims to give developers a clear understanding of the available options for building realtime features and empower them to make informed decisions based on their specific needs.
HN users generally praised the article for its clear explanations and practical approach to building realtime features. Several commenters highlighted the value of the "pull vs. push" breakdown and the discussion of different polling strategies. Some questioned the long-term viability of polling-based solutions and advocated for WebSockets or server-sent events for true real-time experiences. A few users shared their own experiences and preferences with specific technologies like LiveView and Elixir's Phoenix Channels. There was also some discussion about the trade-offs between complexity, performance, and scalability when choosing different realtime approaches.
Scaling WebSockets presents challenges beyond simply scaling HTTP. While horizontal scaling with multiple WebSocket servers seems straightforward, managing client connections and message routing introduces significant complexity. A central message broker becomes necessary to distribute messages across servers, introducing potential single points of failure and performance bottlenecks. Various approaches exist, including sticky sessions, which bind clients to specific servers, and distributing connections across servers with a router and shared state, each with tradeoffs. Ultimately, choosing the right architecture requires careful consideration of factors like message frequency, connection duration, and the need for features like message ordering and guaranteed delivery. The more sophisticated the features and higher the performance requirements, the more complex the solution becomes, involving techniques like sharding and clustering the message broker.
HN commenters discuss the challenges of scaling WebSockets, agreeing with the article's premise. Some highlight the added complexity compared to HTTP, particularly around state management and horizontal scaling. Specific issues mentioned include sticky sessions, message ordering, and dealing with backpressure. Several commenters share personal experiences and anecdotes about WebSocket scaling difficulties, reinforcing the points made in the article. A few suggest alternative approaches like server-sent events (SSE) for simpler use cases, while others recommend specific technologies or architectural patterns for robust WebSocket deployments. The difficulty in finding experienced WebSocket developers is also touched upon.
Summary of Comments ( 228 )
https://news.ycombinator.com/item?id=43659370
HN commenters largely agree with the author's premise that WebSockets are often overused for real-time updates when simpler solutions like HTTP long-polling or Server-Sent Events (SSE) would suffice. Several pointed out the added complexity of WebSockets, both in implementation and infrastructure, with one commenter noting the difficulty in scaling WebSocket connections. The benefits of SSE, particularly its simplicity and native browser support, were highlighted. Some suggested that the choice depends heavily on the specific use case, with WebSockets being more suitable for highly interactive applications like online games, while others argued that even these could be served efficiently with alternatives. A few commenters mentioned the advantages of WebSockets in terms of lower latency and bi-directional communication, but these were generally seen as niche benefits that don't justify the added complexity for most applications. The general consensus seemed to be: consider simpler options first, and only reach for WebSockets when absolutely necessary.
The Hacker News post "You might not need WebSockets" (https://news.ycombinator.com/item?id=43659370) sparked a discussion with several insightful comments. Many commenters agreed with the author's premise that WebSockets are often overused for real-time updates when simpler solutions like HTTP long-polling or Server-Sent Events (SSE) would suffice.
One compelling argument highlighted the added complexity introduced by WebSockets, including connection management, reconnection logic, and handling various edge cases. Commenters pointed out that this complexity can lead to increased development time and potentially more bugs, especially for smaller projects or less experienced developers. They argued that unless bidirectional communication is absolutely necessary, the simpler alternatives are preferable.
Several users shared their personal experiences where they successfully replaced WebSockets with SSE or long-polling, resulting in improved performance and simplified codebases. They emphasized the importance of evaluating the actual requirements before opting for WebSockets.
The discussion also touched upon the performance aspects of each approach. While some argued that WebSockets offer lower latency, others contended that the difference is negligible for many applications, and the overhead introduced by WebSockets can sometimes negate its theoretical advantages. The point was made that the latency introduced by network conditions often dwarfs the differences between these technologies.
Another key takeaway from the comments is the importance of considering the specific use case. For chat applications or highly interactive games, WebSockets might be the best choice. However, for applications that primarily involve server-pushed updates, like stock tickers or notification systems, SSE or long-polling are often more suitable.
A few commenters mentioned the better browser compatibility of SSE and long-polling compared to WebSockets, although this is less of a concern in modern web development.
Overall, the comments on the Hacker News post generally supported the article's claim that WebSockets are often unnecessarily complex and that simpler alternatives should be considered first. The discussion provided practical advice and real-world examples to help developers make informed decisions about the best technology for their real-time applications.