Q56: How do you choose between polling, long polling, Server-Sent Events, and WebSockets?
Polling sends requests at intervals and is simple but may waste traffic and add delay. Long polling holds a request until data arrives or a timeout occurs. Server-Sent Events provide one-way server-to-client streaming over HTTP and are useful for updates. WebSockets provide persistent bidirectional communication and suit interactive chat or collaboration. Consider proxy support, connection scale, reconnect behavior, authentication, and whether the client must send frequent real-time messages.
Key Takeaways
- Use SSE for server-to-client streams and WebSockets for bidirectional interaction.
- Connection lifecycle and reconnect logic are part of the design.