Remember the popular QQ show chat rooms? At that time, we are still in the middle and high school, QQ is the most commonly used chatting and making friends tools; And the QQ show chat room appearance broke the limitation that can search a friend according to the condition only, everybody can enter the chat room room at will, carry on the online chat. Miss that wearing the coolest dazzle OF QQ show I in the chat room shining stage, plain ask 1: “GG/MM?”

The birth of the WebSocket

As the development of the Internet, all sorts of chat room software appeared on the net, all sorts of novel play, can not find the feeling of that pure in those days again however. Of course, real-time communication is the attraction of chat rooms and today’s video barrage. To achieve application scenarios similar to real-time interaction, low latency and high timely technologies are required.

At one time, the technique used by many sites to achieve this scenario was polling. That is, every once in a while (for example, every one second), the browser sends an HTTP request to the server, asking if the server has any new information, and the server returns the latest data to the client. This traditional polling mode has obvious disadvantages, that is, the browser needs to constantly send requests to the server, but HTTP requests contain long headers, and only a small portion of the truly valid data, which obviously wastes some bandwidth resources. Is there a better way?

WebSocket came into being. WebSocket protocol was born in 2008 and became an international standard in 2011, and WebSocket is also a part of the HTML 5 specification. WebSocket is a new protocol, it will TCP Socket (Socket) application in the Web page, so that the communication between the two parties to establish a connection channel to remain in the active state, and belongs to the full duplex (two-way communication between both parties at the same time). WebSocket protocol saves server resources and bandwidth better, and can communicate in more real time.

How is WebSocket different from HTTP?

##HTTP vs WebSocket

HTTP is a half-duplex protocol. That is, data can be transmitted in only one direction at a time. The server is unaware of the connection until the client initiates the connection to the server. A request is made, a response is received, the communication is over, and the client and server “forget each other.” However, it is now possible to keep the client in some state through cookies so that the server can recognize the client.

The WebSocket protocol is full-duplex. The server can actively send data to the client at any time. It can send or receive information bidirectional. WebSocket can establish a connection through a handshake between client and server, and the connection remains “open”, not just one request + one response. First, the client initiates a request to establish a connection, and if the server accepts the request, two-way communication is established. The server and client can then interact with each other until the client or server sends a message to shut it down.

Of course, WebSocket and HTTP are also related. The WebSocket needs to establish a connection through the HTTP 101 status code first. To create a WebSocket connection, you make a request through the browser and wait for the server to respond, a process often referred to as “Handshaking.”

A typical WebSocket handshake request looks like this:

Client request:

GET / HTTP/1.1

Upgrade: websocket

Connection: Upgrade

Host: localhost.com

Origin: localhost.com

Sec-WebSocket-Key: aN3cRrW/n8NuIgdhy2VJFX==

Sec-WebSocket-Version: 13

The server responds:

HTTP / 1.1 101 Switching separate Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: pFDoeB2FAdLlXgESz0UT2v7hp0s=

Sec-WebSocket-Location: ws://localhost.com/

Field Description:

  • The Connection field must be Upgrade, indicating that the client wants to connect to the Upgrade.

  • The Upgrade field must be WebSocket, indicating that you want to Upgrade to the WebSocket protocol.

  • The value of sec-websocket-key is a random string that the server uses to construct a summary of sha-1 information. Add “sec-websocket-key” to a special string “258eAFa5-E914-47DA-95CA-C5AB0DC85B11”, calculate SHA-1, and Base64 encoding, Returns the result as the value of the sec-websocket-accept header to the client. This minimizes the possibility of normal HTTP requests being mistaken for the WebSocket protocol.

  • The sec-websocket-version field indicates the supported WebSocket Version. RFC6455 requires version 13 to be used, and previous drafts should be deprecated.

  • Other fields defined in HTTP, such as cookies, can also be used in Websockets.

Websockets use the uniform identifier of WS or WSS, similar to HTTP/HTTPS. Where WSS indicates WebSocket using TLS. Such as:

ws://localhost.com/api

wss://securelocalhost.com/api

WebSocket uses the same TCP port as HTTP/HTTPS. By default, WebSocket uses port 80. When running over TLS, port 443 is used by default.

The advantage of WebSocket

As a “rising star”, WebSocket has many advantages in its wide application.

– Less waste of resources

After the connection is created, the packet headers transmitted between the server and client are relatively small. Resource waste is significantly reduced compared to HTTP requests that carry complete headers each time.

– More real-time

Because the WebSocket protocol is full-duplex, the server can actively transmit data to the client at any time. Compared to HTTP requests that require a client to initiate a request before the server can respond, the latency is significantly less.

– Keep the long connection

Unlike HTTP, WebSocket needs to create a connection first, making it a stateful protocol. HTTP requests, on the other hand, may need to carry status information (such as authentication) with each request.

– Better binary support

WebSocket defines binary frames, making it easier to process binary content than HTTP.

– Better compression

Compared to HTTP compression, WebSocket, with appropriate extension support, can use the context of previous content to deliver similar data, significantly increasing compression rates.

How to choose different business scenarios

Compared to WebSocket and HTTP, HTTP requests are arguably simpler than WebSocket, but they have limitations. You can select a more appropriate protocol in different scenarios.

Most sites on the Internet now load pages directly, with little interaction beyond clicking to load a new page. There is no need to maintain a long connection in this scenario, and using WebSocket would be too “cumbersome”. Most web pages use static resources such as images, Javascript or CSS files. These static resources need to be cached in order to load faster, and they may not come from the same domain, so it’s much easier to use HTTP.

The HTTP protocol sends a request header for each request, whereas WebSocket only carries the header information in the initial request establishment connection (there is, of course, some overhead in passing the message, but it is small). Therefore, if you want to continuously send multiple messages, it is more resource efficient to use Websocket. If you want to develop an application where the client and server interact continuously, WebSockets are the best choice. Such as:

– Socializing, chatting

Social chat tools and online consultation Windows on websites are characterized by low latency and high timeliness. It uses WebSocket protocol to realize real-time communication and meet the needs of communication in an efficient way.

– a barrage

When watching a video, bullet screen is indispensable. Wonderful comments, funny jokes, real-time interaction between netizens, sometimes for a video, bullet screen is the essence. However, the bullet-screen needs to be displayed in real time. Through WebSocket protocol, the bullet-screen information sent by the local client can be pushed to the client of other users through the server and displayed in real time.

– Online education

In recent years, online education has developed rapidly, and you can communicate with teachers and classmates in real time without going out. Teacher assignment, student interaction, teacher consultation and so on can be carried out online in real time, such real-time interaction can be supported by WebSocket protocol.

– Application of location information update

The current mobile device real-time location, real-time network data update, the use of HTTP protocol is a little awkward, using WebSocket can make real-time data update faster.

The cloud WebSocket service product breaks through the limitation that traditional CDN manufacturers can only accelerate HTTP/HTTPS protocol, integrates WebSocket protocol with CDN, and adopts various optimization technologies combined with its own years of technical experience in the CDN industry. For customers using WS/WSS protocol for communication to provide quality acceleration services, can effectively reduce delays, improve efficiency. Come and get the first taste!

Recommended reading

Talk about WebSocket. 3 minutes to see it all

Why is HTTPS more secure than HTTP