What is WebSocket and how is it different from HTTP?

What is WebSocket and how is it different from HTTP?

HTTP

HTTP is one-way, the client sends a request and the server sends a response. For example, when a user sends a request to a server, the request is in the form of HTTP or HTTPS. After receiving the request, the server sends a response to the client. Each request is associated with a corresponding response. After sending the response, the connection is closed. Each HTTP or HTTPS request will establish a new connection to the server, and after getting the response, the connection will terminate itself.

HTTP is a stateless protocol that runs on top of TCP, a connection-oriented protocol that guarantees delivery of packets using a three-way handshake method and retransmits lost packets.

HTTP can run on top of any reliable connection-oriented protocol (e.g. TCP, SCTP). When a client sends an HTTP request to a server, a TCP connection is opened between the client and the server. After getting a response, the TCP connection is terminated. Each HTTP request opens a separate TCP connection to the server. For example, if a client sends 10 requests to a server, 10 separate TCP connections will be opened and closed after getting a response/backoff.

HTTP message information encoded in ASCII. Each HTTP request message includes HTTP protocol version (HTTP/1.1, HTTP/2), HTTP method (GET/POST, etc.), HTTP header (content type, content length), host information, etc., and the body containing the actual message being transmitted to the server. The size of the HTTP header varies from 200 bytes to 2KB, and the common size of the HTTP header is 700-800 bytes. When web applications use more cookies and other tools on the client side to expand the storage capabilities of the proxy, it reduces the HTTP header load.

Figure 1 HTTP connection diagram

WebSocket

WebSocket is a bidirectional, full-duplex protocol used for the same client-server communication scenarios, and unlike HTTP, it starts with ws:// or wss://. It is a stateful protocol, which means that the connection between the client and the server will remain valid until terminated by either party (client or server). Once the client and server close the connection, the connection will be terminated from both ends.

Let's take an example of client-server communication, there is a client which is a web browser and a server, whenever we initiate a connection between the client and the server, the client-server does a handshake and decides to create a new connection which will remain valid until either of them terminates. When the connection is established and active, the same connection channel is used for communication until the communication is terminated.

That is how after the client-server handshake, the client-server decides on a new connection to keep it alive, this new connection will be called WebSocket. Once the communication link is established and the connection is open, the message exchange will be carried out in a bidirectional mode until the connection between the client-server persists. If any of them (client server) goes down or decides to close the connection, both parties will close the connection. The way sockets work is slightly different from the way HTTP works, and the status code 101 indicates the exchange protocol in WebSocket.

Figure 2 WebSocket connection diagram

PART.01 When to use WebSocket

1. Real-time Web Applications: Real-time web applications use Web sockets to display data on the client side, which is continuously sent by the backend server. In WebSocket, data is continuously pushed/transferred to the same connection that is already open, which is why WebSocket is faster and improves application performance. For example, in a trading website or Bitcoin trading, in order to display price fluctuations and movement data, the backend server continuously pushes to the client using a WebSocket channel.

2. Game Applications: In game applications, you may pay attention to this. The server continuously receives data and it will take effect on the screen without refreshing the UI. The UI is automatically refreshed even without establishing a new connection, so it is very useful in game applications.

3. Chat Application: Chat application uses WebSocket to establish a connection only once to exchange, publish, and broadcast messages between subscribers. It reuses the same WebSocket connection for sending and receiving messages and for one-to-one message transmission.

PART.02 When not to use WebSocket

If we want to transmit any real-time updates or continuous stream of data over the network, we can use WebSocket.

If we only want to get data once to process it with the application, we should use HTTP protocol. Data that is only obtained once can be queried by simple HTTP request, so in this case, it is better not to use WebSocket.

<<:  [Important Event] Be neither arrogant nor give up, stick to the original intention, the 2023 Jinrui Marketing Award works collection is officially launched

>>:  TCP send window, receive window and how they work

Recommend

The 10 coolest software-defined networking technologies of 2017

Software is driving the innovation engine of the ...

2017 Prediction: Will Networking and Security Finally Merge?

[51CTO.com Quick Translation] It’s a new year aga...

ENOs and Private LTE: Intelligent Connectivity for Smart Factories

Manufacturing processes and operations are underg...

Edge computing vs. cloud computing: Which is more efficient?

Cloud computing provides businesses with the oppo...

Huawei's Meng Wanzhou: 5.5G is the inevitable path for 5G network evolution

On June 28, 2023 MWC Shanghai opened, and Huawei ...

Network Slicing: A Booster for 5G

Preface I have recently become interested in 5G n...

India issues 5G trial license, but won't use Chinese network technology

The decision to exclude the Chinese manufacturer ...

Mobile Edge Computing: The True Future of 5G

The promise of 5G has yet to be fulfilled, but it...

As VR enters its heyday, how will the three major operators plan their layout?

As we all know, VR is a very popular technology n...

After 5G technology, there may not be 6G base stations! Why do you say that?

When the 4G technology standards were announced, ...