“WebSocket is an advanced technology that opens an interactive communication session between the user’s browser and the server. With WebSocket, you can send a message to the server and receive the response in real time, rather than polling the server to get the response on the server in the traditional way.”

This is an introduction to WebSocket from Mozilla’s developer documentation page. In simple terms, WebSocket allows the browser to remain connected to the server for a period of time. It enables full-duplex communication between the browser and the server, which allows the server to actively send messages to the client. This feature is especially friendly for scenarios that require real-time data exchange, such as dynamically updating location data, pulling hot news, building high-performance games in the browser, and collecting more clickstream data.

What problem does WebSocket solve

I believe we all have encountered such a scenario, some resources on the server will often be updated, the client needs to try to timely obtain these updates, in the traditional HTTP protocol, if the client does not initiate a Request, then the server is not able to initiate data to the client. So how do we solve the data update problem between client and server?

A common approach is AJAX polling. The client sets a timer, and within a certain period of time the client will make an AJAX request to the server, asking if there is an update, and if there is an update, the data will be returned in time. Through the timer, the client can repeatedly poll the relevant resources on the server for updates, so as to achieve nearly “real-time” communication.

AJAX polling reduces the latency of data synchronization between the client and server, but it also introduces some performance costs. , for example, if the client is more, so the server receives polling request at the same time will increase, this will cause great pressure to the server, traffic generated by polling, also have a certain consumption of network, the other server resources update frequency is relatively frequent, timer interval is larger, but the client or server for a long time not updated resources, This can lead to problems such as delayed resource updates and bandwidth consumption.

But with WebSocket, the browser and the server only need to complete a handshake in the WebSocket API, the two can directly create a persistent connection and two-way data transfer. This allows the server to communicate back to the client even if there is no explicit request from the client. This is “full duplex” communication between the server and the client.

Click here to experience the magic of WebSocket.

How WebSocket works

The client (or multiple clients) starts by sending an HTTP request to the server, which includes the WebSocket supported version number, protocol version number, original address, host address, and other fields to the server, indicating to the server that the client is trying to establish a WebSocket connection.

If the server checks that the data and format of the data packets are correct and the protocol version of the client and server match, the server accepts the handshake connection and sends corresponding data reply, which is also transmitted through HTTP. Upon client confirmation, the initial HTTP connection is upgraded to a WebSocket connection and maintained for each client, enabling two-way communication.

The advantage of WebSocket

Compared with the traditional polling method, WebSocket can save server resources and bandwidth better, and can carry out more real-time communication. The advantages are as follows:

  • Reduce bandwidth overhead. After the connection is established between the server and the client, the packet header used for protocol control is relatively small compared with HTTP request, usually only 2 bytes.

  • Enhance real time. The server can actively send data to the client at any time. Compared with THE HTTP request, the server can only respond to the request initiated by the client, and the delay is significantly less. Compared with the traditional polling, WebSocket can also transmit data more efficiently in a short time.

  • Maintain the connection status. In some scenarios need to identity authentication, HTTP requests may need to carry on every request status information (server does not record every time request and response information), and after a connection is established the WebSocket will maintain session state, which makes it become a stateful protocol and subsequent communications can omit part of the state information;

  • More flexible extension support. According to THE RFC6455 protocol, developers can customize binary frames for WebSocket, which can handle binary content more easily than HTTP. In addition, developers also extend the protocol and implement some customized sub-protocols.

  • Better compression. WebSocket, with appropriate extension support, can continue the context of previous content, which can significantly improve compression when passing similar data.

With all of WebSocket’s advantages, where does it fit in? Here is a simple understanding:

  • Scenarios that require timely responses. WebSocket is ideal when the client needs to respond quickly to changes on the server, especially if the client cannot predict the response. For example, developing a customer service system often requires real-time communication between multiple users. With WebSocket, each conversation can send and receive messages in real time. Compared to HTTP, WebSocket does not need to consider the overhead of HTTP request/response for each message sent and received, resulting in higher execution efficiency.

  • Scenarios that require real-time query. For example, if a basketball fan wants to query the results of a game, HTTP is great for situations where the results are fixed if the game ended last week. However, if the game is currently in progress, the score is constantly changing and updated frequently, in which case WebSocket is a better choice.

  • High-frequency messaging with small loads. More and more developers are using GPS capabilities on mobile devices to record the location awareness of Web applications. If we need to record a user’s location over a period of time, sending more fine-grained location data at a high frequency for real-time sharing (such as sports apps), the TCP connection used by WebSocket will make the data exchange fly.

  • Multi-player collaborative scenarios. Such as the online education is developing rapidly in recent years, students can never leave home, can communicate with teachers and other students and the exchange, such as homework, interaction between teachers and students, discussion, and so on strong real-time interactive classes can be passed on to the WebSocket protocol supporting complete, to meet the low latency, high timely scene.

At present, Youpai Cloud has been able to support WebSocket seamless access, relying on Youpai CLOUD CDN 1100+ global nodes, 10Tbps bandwidth reserve, domestic mainstream operators support, through TCP protocol optimization, link optimization, content optimization, intelligent scheduling and other technical means, greatly improve the acceleration performance. Self-service configuration management, configuration policies take effect within 10 seconds across the network; Provide a full range of API interface, support diversified management, simple configuration, you can quickly access, enjoy the whole station acceleration.

Recommended reading

Happy big pu Ben, shoot cloud’s new product WebSocket online

The world has run out of 4.3 billion IPv4 addresses! IPv6, now