WebSocket Usage Scenarios
Social chat, pop-up, multiplayer, collaborative editing, real-time quotes for stock funds, live updates, video conferencing/chat, location-based applications, online education, smart home, etc. need high-real-time scenarios
By polling to WebSocket
1 polling
Connections are always made between the client and the server and are queried every once in a while. The client polls and there are no new messages. This way the number of connections will be many, one to accept, one to send. And each send request will have HTTP header, will be very consumption of traffic, will also consume CPU utilization.
2 Long polling
Long polling is an improved version of polling, after the client sends HTTP to the server, there is no new message, and if there is no new message, it waits. When new messages are available, they are returned to the client. To some extent, it reduces the network bandwidth and CPU utilization and so on. However, this approach still has a disadvantage: for example, assuming that the server side of the data update is very fast, the server after the delivery of a packet to the client must wait for the client's next get request to arrive, in order to pass the second updated packet to the client, then the client display real-time data the fastest time is 2x RTT (round trip time), and if in the case of network congestion, this time user is unacceptable, such as in the stock market quotes. In addition, because HTTP packet header data is often large (usually more than 400 bytes), but the actual data required by the server is very small (sometimes only 10 bytes or so), such a packet on the network periodic transmission, it is inevitable that network bandwidth is a waste.
3 WebSocket
The need now is to support the client and server side of the two-way communication, and the head of the protocol is not the same size as the HTTP header, so, WebSocket was born! Traffic consumption, the same number of client polling per second, when the number of times up to tens of thousands of high frequency per second, websocket consumption of traffic is only a few 1% polling
WebSocket protocol principle
WebSocket is an application layer protocol on the seventh layer of the application layer, which must rely on the HTTP protocol for a handshake, and after the handshake succeeds, the data is transmitted directly from the TCP channel, regardless of the HTTP.
The data transmission of WebSocket is transferred in frame form, for example, a message is divided into several frames and transmitted in sequence. There are several benefits to doing so:
1 The transmission of large data can be fragmented, regardless of the size of the data caused by the length of the flag bit is not enough.
2, like HTTP chunk, can generate data side-by-side delivery message, that is, improve transmission efficiency.
The difference and connection between WebSocket and sockets
First, the Socket is not actually a protocol. It works at the OSI model Session layer (layer 5th), which is a layer of abstraction that exists to facilitate direct use of the lower level protocol (typically TCP or UDP). Socket is the encapsulation of TCP/IP protocol, the socket itself is not a protocol, but a calling interface (API).
Sockets are also commonly referred to as "sockets," which describe IP addresses and ports, and are a handle to a communication chain. The two programs on the network realize the exchange of data through a two-way communication connection, one end of this bidirectional link is called a socket, and a socket is determined only by an IP address and a port number. Applications typically make requests to the network through sockets or answer network requests.
In the communication process, the server listens to a port for connection requests, the client sends a connection request to the server, and the server receives a connection request to send a message to the client, so a connection is established. Both the client and the server can also send messages to each other and communicate with each other until the connection is disconnected
WebSocket principle and use scene [reprint]