Hypertext Transfer Protocol (HTTP) — 1.0, 1.1, 2.0, and WebSockets

Mohit Sharma
6 min readDec 27, 2023

HTTP is the protocol used for transmitting data over the internet. There are three versions of HTTP: 1.0, 1.1, and 2.0.

HTTP 1.0 was the first version of HTTP and was released in 1996. It was a simple protocol that allowed clients to request data from servers and servers to respond with that data. However, it had some limitations, such as only allowing one request per connection and not supporting caching.

HTTP 1.1 was released in 1999 and addressed many of the limitations of HTTP 1.0. It introduced persistent connections, which allowed multiple requests to be sent over a single connection, and pipelining, which allowed multiple requests to be sent without waiting for a response. It also introduced caching and improved support for proxies.

HTTP 2.0 was released in 2015 and is the latest version of HTTP. It was derived from the earlier experimental SPDY protocol. It is a major revision of the protocol and introduces several new features, such as multiplexing, which allows multiple requests to be sent and received at the same time over a single connection. It also introduces server push, which allows servers to send data to clients before the client requests it.

  1. Multiplexing: HTTP/2 allows multiple requests to be sent over a single connection, which can improve performance by reducing the number of connections required.
  2. Header compression: HTTP/2 uses HPACK compression to reduce the size of headers, which can improve performance by reducing the amount of data that needs to be sent.
  3. Server push: HTTP/2 allows servers to push resources to clients before they are requested, which can improve performance by reducing the number of round trips required.
  4. Stream prioritization: HTTP/2 allows clients to prioritize streams, which can improve performance by ensuring that high-priority requests are processed first.

HTTP/2 is designed to be backwards-compatible with HTTP/1.1, so existing applications can continue to work without modification. However, some changes may be required to take full advantage of the new features.

HTTP Message Format

HTTP messages are the format used for transmitting data over the internet. There are two types of HTTP messages: request messages and response messages.

  1. Request messages are sent by clients to request data from servers. They consist of three parts: a request line, headers, and an optional message body. The request line contains the method (such as GET/POST/PUT/DELETE/PATCH/HEAD), the URI (Uniform Resource Identifier) of the resource being requested, and the HTTP version being used. The headers contain additional information about the request, such as the user agent (the software used to make the request) and any cookies that should be sent with the request. The message body contains any data that should be sent with the request, such as form data.
  2. Response messages are sent by servers in response to requests from clients. They consist of three parts: a status line, headers, and an optional message body. The status line contains the HTTP version being used, a status code (such as 200 OK or 404 Not Found), and a reason phrase (a short description of the status code). The headers contain additional information about the response, such as the content type (the type of data being sent) and any cookies that should be set by the server. The message body contains any data that should be sent with the response, such as HTML or JSON data.

Representational State Transfer (REST)

REST stands for Representational State Transfer and is an architectural style based on the Hypertext Transfer Protocol (HTTP) for developing web-based applications. REST outlines several guidelines that web services must follow to be considered RESTful. RESTful services focus on system resources and how the state of a resource should be transported over HTTP protocol to different clients written in different languages.

Restful web services are

  1. Lightweight, highly scalable, and maintainable.
  2. Stateless, meaning that each request from the client to the server must contain all the necessary information to understand the request. The server cannot store any context about the client.
  3. Use standard HTTP methods such as GET, POST, PUT, DELETE, and PATCH to perform operations on resources.
  4. Use HTTP status codes to indicate the success or failure of an API call.
  5. Can be used with any programming language and can be used with any data format such as JSON or XML.

Asynchronous Communication

There are several ways to achieve asynchronous communication between a client and server. Here are some of the most common methods:

  1. Polling: The client sends a request to the server at regular intervals to check for new data. This method is simple to implement but can be inefficient if there is no new data.
  2. Long-polling: The client sends a request to the server and waits for a response. If there is no new data, the server holds the connection open until new data is available. This method reduces the number of requests but can still be inefficient if there is no new data.
  3. Server-Sent Events (SSE): The server sends data to the client over a persistent connection. This method is efficient and easy to implement but only supports one-way communication from the server to the client.
  4. WebSockets: The client and server establish a persistent connection over which they can send data in both directions at any time. This method is efficient and supports bidirectional communication between the client and server.

Web 2.0: Bidirectional client / server communication

Web 2.0 is a term used to describe the second generation of the World Wide Web that emphasizes the ability for people to collaborate and share information online . One of the key features of Web 2.0 is bidirectional client/server communication, which allows web applications to behave more like desktop applications . This is achieved through the use of technologies such as AJAX (Asynchronous JavaScript and XML) and WebSockets.

Bidirectional client refers to the ability of a client to send and receive data from a server. This is in contrast to unidirectional communication, where data can only be sent in one direction (from the client to the server or vice versa). In the context of Web 2.0, bidirectional client/server communication allows web applications to behave more like desktop applications by enabling real-time updates and interactivity.

WebSockets

WebSockets are a protocol for bidirectional communication between a client and a server over the web. They allow for real-time communication between the client and server without the need for polling or long-polling. WebSockets work by establishing a persistent connection between the client and server, which allows for data to be sent in both directions at any time.
The WebSocket protocol is designed to work over HTTP and uses the same ports (80 and 443) as HTTP and HTTPS. The WebSocket handshake is similar to the HTTP handshake, but includes an “Upgrade” header that tells the server that the client wants to establish a WebSocket connection. Once the connection is established, data can be sent in both directions using the WebSocket protocol.

Step 1: Opening Handshake The WebSocket handshake is the process by which a client and server establish a WebSocket connection. The handshake is similar to the HTTP handshake, but includes an “Upgrade” header that tells the server that the client wants to establish a WebSocket connection.
Here is an example of what the WebSocket handshake looks like:

GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13

Once the server receives this request, it sends back a response that looks like this:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

This response tells the client that the server has accepted the WebSocket connection and that data can now be sent in both directions using the WebSocket protocol.

Step 2: Data Transfer

  • Bidirectional communication
  • WebSocket frame
  • Description of fields: Op-code: Continuation, Text, Binary, Close, Ping-Pong (To keep connection alive)

Step 3: Closing handshake
• A WebSocket frame with opcode 0x8 is sent.

Most cloud providers support WebSockets:

  • Amazon Web Services (AWS) supports WebSockets through its API Gateway service.
  • Microsoft Azure supports WebSockets through its SignalR service.
  • Google Cloud Platform (GCP) supports WebSockets through its Cloud Run and App Engine services.
  • Slack and its “Real Time Messaging API”

--

--