What is the principle of WebSocket? Why can it achieve persistent connection?

What is the principle of WebSocket? Why can it achieve persistent connection?

[[396397]]

To better understand WebSocket, we need to start with HTTP, because many people like to compare WebSocket with HTTP.

The life cycle of HTTP is defined by Request. With one Request and one Response, the HTTP request ends.

In HTTP1.1, sending multiple Requests and receiving multiple Responses are allowed, but no matter what, a Request is always equal to a Response.

In order to achieve real-time information transmission, many websites use polling technology. There are two types of polling: ajax polling and long polling.

The principle of Ajax polling is very simple. It allows the browser to send a request to the server every few seconds to ask the server whether there is new information.

Client: Is there any new information? If not, I will ask again later (Request)

Server: No (Response)

Client: Is there one? If not, I will ask again later (Request)

Server: No (Response)

Client: Is there any new information? If not, I will ask again later (Request)

Server: Yes, I sent it to you (Response)

Long polling is similar to ajax polling, but the difference is that when the client initiates a request, if there is no message, no Response will be returned until there is a new message.

Client: Is there any new information? If not, please don’t reply to me (Request)

Server: Now I have it, here it is (Response)

The above two methods are constantly establishing HTTP connections. The server can only respond when the client initiates a request. The server cannot actively initiate a request.

Moreover, HTTP is a stateless protocol. The server does not retain any state during transactions with the client. The advantage of this is that it greatly reduces the memory burden of the server and maintains a faster response speed.

However, this also creates some problems. The client has to continuously send HTTP requests, each with a long header, which may result in very little valid data, wasting a lot of bandwidth resources and adding pressure to the server.

Well, our protagonist WebSocket is finally ready to appear.

Compared with HTTP, WebSocket is a persistent protocol. It will complete part of the handshake based on the HTTP protocol, and then it will break away from HTTP and completely adopt WebSocket.

The process of establishing a WebSocket connection is:

  • The client initiates an HTTP request, and after three handshakes, a TCP connection is established. The HTTP request stores information such as the version number supported by WebSocket.
  • After receiving the handshake request from the client, the server returns data.

After completing the above two steps, the HTTP handshake part is completed and the protocol is upgraded to WebSocket. At this time, the server no longer needs the client to initiate requests and respond to requests, and can actively push information to the client.

Client: I want to establish the WebSocket protocol.

Server: OK, it has been upgraded to WebSocket protocol.

Client: When there is information, push it to me.

Server: OK, we will send you the information.

Server: I have information. Here it is.

Server: There is more information. Here it is.

With just one HTTP request, the server can continuously push information to the client!

The client actively asks and the server actively pushes the information, which solves the problem of excessive resource consumption on the server.

The above is the principle of WebSocket and the process of achieving persistent connection.

<<:  Common network problem location tools that programmers should master

>>:  The so-called ICMP is nothing more than a general and soldiers

Recommend

How is the VoLTE development of the three major operators?

There are always various opportunities for indust...

IDC: Ethernet switch market grows 2%

According to IDC's Worldwide Quarterly Ethern...

Foreign media: As of June, South Korea's 5G users have exceeded 16 million

On August 11, according to foreign media reports,...

How 5G contributes to Industry 4.0

During the COVID-19 pandemic, industries across t...

What is the function of each layer in the computer network layered model?

1. Layering of computer networks In the computer ...

Ethernet IP: Unlocking the power of high-speed data transmission

Ethernet IP has revolutionized the world of netwo...

A brief discussion of the TCP protocol, finally understand what it does

[[276056]] 1. What is TCP and what does it do? TC...

How businesses can prepare for 5G

[[355718]] While people may think of 5G as a cool...