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

HOSTEROID: €1.5/month KVM-512MB/10GB/324GB/Vienna & New Jersey & London

HOSTEROID is a British hosting company founded in...

The most anticipated technology trends in 2023

In 2023, technology will further develop, and new...

The 5G Revolution: Unveiling the Future of Healthcare

Evan Kirstel, Chief Digital Evangelist and Co-Fou...

Seven development tools for continuous integration and continuous delivery

[[184286]] The software development cycle require...

Why is your router's ability to penetrate walls poor?

1. Is it my fault that the signal is weak? Whethe...

...

The troublemakers that kept IT executives up at night in 2017

[51CTO.com Quick Translation] The new year has ar...

Want to handle tens of millions of traffic? You should do this!

Your boss asks you to handle tens of millions of ...

Inventory: 11 foreign SASE vendors

SASE (Secure Access Service Edge) is a networking...

Selection of the most influential events in the communications industry in 2020

Looking back at the year 2020, there are many eve...