WebSocket

WebSocket is not built directly on top of HTTP, but it typically starts as an HTTP or HTTPS connection and then "upgrades" to the WebSocket protocol through a process called the WebSocket handshake.

Here's how it works:

  1. WebSocket Handshake: The WebSocket handshake begins with an HTTP or HTTPS request from the client to the server. This request includes an "Upgrade" header indicating that the client wants to switch to the WebSocket protocol. If the server supports WebSocket, it responds with an HTTP 101 status code (Switching Protocols) along with the necessary headers to indicate the switch to the WebSocket protocol.

  2. WebSocket Protocol: Once the WebSocket handshake is complete, the communication transitions to the WebSocket protocol. WebSocket is a separate, full-duplex communication protocol that operates over a single, long-lived TCP connection. It provides a way for both the client and server to send messages to each other asynchronously without the overhead of traditional HTTP request-response cycles.

While WebSocket connections typically begin with an HTTP request, WebSocket itself is a separate protocol designed to provide efficient, low-latency, bidirectional communication between clients and servers. It's often used in scenarios where real-time or interactive communication is required, such as chat applications, online gaming, and real-time data visualization.

Last updated