Last updated on

Websockets

🌐 Limitations of HTTP and the Need for Alternatives

The Hypertext Transfer Protocol (HTTP) is satisfactory for most web interactions, allowing clients to send requests (e.g., loading a webpage, fetching data) and receive responses from a server.

However, when it comes to real-time communication, like a live chat application (e.g., Twitch chat), HTTP has limitations. A key problem in implementing real-time updates with standard HTTP is:

  • Polling: To get the newest information (e.g., new chat messages), the client must repeatedly send new HTTP requests to the server at fixed intervals.
    • The Issue: Polling is inefficient. If the interval is too long (e.g., one minute), the user experiences a significant delay. If the interval is too short (e.g., one second), the client must continuously create new, somewhat expensive TCP connections for each request, leading to less optimized performance and unnecessary overhead.

πŸš€ Introducing WebSockets: A Solution for Real-Time

WebSockets is a protocol designed specifically to address the limitations of HTTP for applications requiring real-time, bi-directional communication.

WebSocket Handshake

Establishing a WebSocket connection begins with a process called the WebSocket handshake:

  1. The client sends an initial HTTP request to the server.
  2. This request signals the client’s intent to establish a WebSocket connection.
  3. The server responds with a status code of 101 (Switching Protocols), indicating that it is upgrading the existing connection to a WebSocket connection.

Persistent, Bi-Directional Communication

Once the handshake is complete, a persistent connection is established between the client and the server.

  • HTTP Model: Client initiates request $\rightarrow$ Server sends single response $\rightarrow$ Connection closes (or is reused for another request-response cycle).
  • WebSocket Model: A single, sustained connection allows full-duplex (bi-directional) communication.
    • Data can move from the client to the server (e.g., the user typing a new message).
    • Data can be pushed from the server to the client (e.g., the server immediately sending a new message from another user), eliminating the need for the client to constantly poll.

Benefits of WebSockets

  • Real-Time Data Streaming: New data is immediately pushed to the client, ideal for live feeds, stock tickers, or chat.
  • Reduced Overhead: Only one connection is established (via the handshake), avoiding the cost of creating a new connection for every update, as is required with polling.
  • Use Case: WebSockets are highly effective for applications where data updates are frequent and immediate delivery is crucial, such as the Twitch chat example.

Note on HTTP/2 and HTTP/3: While WebSockets are highly prevalent, it is worth noting that modern HTTP versions (specifically HTTP/2, which introduced streaming) can technically make WebSockets obsolete by offering similar persistent, bi-directional capabilities. However, WebSockets remain widely used and relevant in industry.


πŸ—‚οΈ Overview of Application Layer Protocols

WebSockets and HTTP are two examples of Application Layer Protocols. Many others exist, often tailored to specific tasks, and most are built on top of the Transmission Control Protocol (TCP) for reliability.

ProtocolFull NameDefault Port (By default)PurposeTransport Layer Protocol
HTTPHypertext Transfer Protocol80Web page and resource loading (client-initiated request/response).TCP
WebSockets (WS)WebSocket ProtocolN/A (Upgrades HTTP)Persistent, bi-directional real-time communication.TCP
FTPFile Transfer Protocol21Transferring files between computers.TCP
SMTPSimple Mail Transfer Protocol25Sending email messages.TCP
SSHSecure Shell ProtocolN/A (Often 22)Securely connecting to and running commands on a remote machine.TCP
WebRTCWeb Real-Time CommunicationN/AVideo and audio streaming.UDP

Key Takeaway: While most protocols listed rely on TCP for its reliability features, WebRTC uses UDP (User Datagram Protocol) because audio and video streaming prioritizes speed and low latency over guaranteed delivery of every single data packet, accepting occasional packet loss for a smoother real-time experience.