WebSocket? Many websites use polling to implement push technology. Polling is that at a specific time interval (such as 1 sec), the browser sends an HTTP request to the server, and then the server returns the latest data to the browser. The disadvantages of voting are obvious. Browsers need to constantly send requests to the server. However, the header of the HTTP request is very long, and the actual transmitted data may be very small, resulting in a waste of bandwidth and server resources.
Comet uses AJAX to improve polling and can realize two-way communication. But Comet still needs to send requests, and in Comet, long links are widely used, which will also consume a lot of server bandwidth and resources.
Therefore, WebSocket protocol came into being. The browser sends a request for establishing WebSocket connection to the server through JavaScript. After the connection is established, the client and the server exchange data directly through TCP connection. WebSocket connection is essentially a TCP connection.
WebSocket has great performance advantages in the stability and size of data transmission. Websocket.org compared the performance advantages of polling and WebSocket:
HTTP rotation training needs to return 87 1 byte each time, and websocket only needs 2 bytes each time.
Use case A: 1000 clients receive one message per second, and the network throughput (2 * 1000)= 2000 bytes = 16000 bits per second.
Use case B: 10000 clients receive one message per second, and the network throughput (2 * 10000)= 20000 bytes = 160000 bits per second.
Use case C: 100000 clients receive a message per second, and the network throughput (2 * 100000)= 200000 bytes = 1 600000 bits per second.
MQTT protocol is designed for communication between a large number of remote sensors and control devices, which have limited computing power and work in low-bandwidth and unreliable networks. It has the following main features:
The communication overhead is very small (the minimum message size is 2 bytes), the transmission volume is small, the overhead is very small (the fixed-length header is 2 bytes), the protocol exchange is minimized, and the network traffic is reduced.
Easy-to-use client, supporting various popular programming languages (including C, Java, Ruby, Python, etc. );
Use publish/subscribe message mode to provide one-to-many message publishing and separation applications.
Mask the message transmission of payload content.
Use TCP/IP to provide network connection.
There are three kinds of quality of service for message publishing, so that messages can reach their destinations on demand and meet the transmission requirements of unstable networks:
"At most once", message publishing depends entirely on the underlying TCP/IP network. Message loss or duplication may occur. This level can be used in the following situations. It doesn't matter if the environmental sensor data is lost once, because there will be a second transmission soon.
"At least once" to ensure that the message arrives, but message duplication may occur.
"Only once" ensures that the message arrives once. This level can be used when repeated or lost messages will lead to wrong results in the billing system.