What is WebSocket, where is it used, and when would you choose it instead of ordinary HTTP, polling or Server-Sent Events?
Answer
WebSocket is a persistent full-duplex protocol that lets both client and server send messages at any time over one long-lived connection. It is useful for chats, live notifications, collaborative editing, games, dashboards, trading feeds, telemetry and other interactive low-latency features. Ordinary HTTP is better for normal request/response CRUD. Polling is simpler for infrequent updates, while SSE is often sufficient when only the server needs to push events to a browser. The choice should follow communication direction, frequency, latency and operational complexity rather than the label real-time.
Strong answer includes
- persistent bidirectional connection
- concrete real-time use cases
- compares WebSocket with polling and SSE
Practical example
A dashboard refreshes once every five minutes, so replacing a normal HTTP request with WebSocket would add connection management without much benefit. A collaborative editor receiving edits from many users continuously is a much stronger WebSocket case.