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

iPhone 12 will be released next week, but 5G in the US won't work at all

Apple announced in the early morning of October 7...

MESI protocol, JMM, common thread methods, etc.

[[329428]] This article is reproduced from the We...

ASUS releases PG27VQ gaming monitor: 165Hz, RGB light

RGB lights have now spread to every corner inside...

How to design a distributed ID generator?

Hello everyone, I am Brother Shu. In complex dist...

As of December 2020, there are 229 million 5G users worldwide

On December 22, according to foreign media report...

Let’s talk about the brief history of world communications

This article is reprinted from the WeChat public ...

5G is here, what can 5G do?

5G is here. In order to let everyone know clearly...