HTTP request-response versus WebSocket connections?
understanding request-response versus persistent bidirectional models.
HTTP is request-initiated, pull-based, client waits for response. WebSocket is persistent, bidirectional, either side sends data anytime.
HTTP REQUEST-RESPONSE MODEL
HTTP is stateless and request-driven. A client sends a request, the server processes it, sends a response, and the connection closes. The server cannot initiate communication; it only reacts. For live chat, the server would need to wait for the client to repeatedly ask, Is there a new message? via polling. This is inefficient and high-latency.
WEBSOCKET PERSISTENT CONNECTION
WebSocket upgrades the initial HTTP connection into a persistent bidirectional tunnel over TCP. Once established, both client and server can send data at any time without request-response framing. The connection stays open, allowing instant message delivery. The WebSocket handshake uses HTTP upgrade headers, but the protocol is completely different after that.
WHY WEBSOCKETS FOR LIVE CHAT
In a live chat, multiple participants send and receive messages. With HTTP, either the client polls continuously (wasteful and delayed) or the server polls client connections (impossible; server cannot initiate). WebSocket solves this: Server sees new message, immediately pushes to all clients subscribed to the chat room. Clients see messages within milliseconds, no polling delay.
EFFICIENCY COMPARISON
HTTP polling generates request headers and response metadata with every check, even if no new message exists. For 100 users polling every second, that is millions of headers per hour. WebSocket sends only actual data: a few bytes per message. Latency is sub-100ms for WebSocket versus seconds for polling, and server load is orders of magnitude lower.
WHEN HTTP IS FINE
HTTP is appropriate for request-response operations where the client asks for something specific. Getting user profile data, uploading a file, or paginating results fit the model. Mixed architectures use HTTP for setup and control (auth, configuration) and WebSocket for real-time data (chat, notifications, live feeds).
Read the original → developer.mozilla.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.