Introduction
The first time I heard about MQTT, I was trying to find a solution for mobile push. Later I found that JPush is really good, and then I won't make any effort. Recently, I suddenly felt a whim, I read the MQTT protocol, and there are manyChineseAll the materials are pitfall, all of which are MQTT-based push. All of my photos are reproduced and translated from this article written by foreigners 2010... later, I switched to the full-English keyword, And the search finally found the use of MQTT. if you don't want to read my long article, I suggest you go to the wiki to view MQTT details.
What does MQTT do?
What do we need to know? What do we need to solve? From the perspective of the WIKI, The MQTT protocol mainly solves the problem of data communication between machines. What do you think? If you have been in touch with IOT, you may have some knowledge. When all our machines can allocate an address on the same network, the performance difference between devices may be as low as a socket, however, you need the socket to enable data communication. For example, to control the open and closed sockets, an extremely lightweight protocol is required, and the MQTT protocol is born for this purpose.
Interestingly, the latest version of MQTT was v3.1 (2010/12/06) in 1999. It applies to the following but is not limited to the following:
Simple MQTT read
Compared to some protocols, the MQTT protocol is really short and horrible. The entire protocol is finished on only 42 pages.
There are several important changes in MQTT v3 to v3.1. I personally feel that the most important thing is to switch from ascii code to utf8 support. However, I don't think I have used v3, if you are interested, please refer to the agreement document .....
Comparison of transmission overhead
MQTT is most proud of the minimum 2 byte header transmission overhead. Let's take a look at the message format design of other popular protocols.
There are many other protocols, so let's talk about them differently. Let's look at the two Protocols that I know better. currently, common protocols have very few lower transmission overhead than MQTT. if you want to know more about it.
Brief Introduction to Message Body Design
Bit |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Byte 1 |
Message Type |
DUP flag |
QoS level |
RETAIN |
Byte 2 |
Remaining Length |
The first byte is used to describe the message body information.
The second byte is used to transmit the data we need to transmit.
For more details, see the msg-format section of the Protocol.
Next, we will describe the message body with the simplest example.
Simplest MQTT example
In order to facilitate the understanding and use of MQTT, currently there is a shortage of MQTT materials and no powerful examples can be found. Therefore, with the depth of my research, I will gradually increase the difficulty of these examples.
Preparation
Server:
Client:
- Nodejs: MQTT. js
- Java: Paho
Example address: https://github.com/youxiachai/mqttlesson/tree/master/LessonOne java version is not provided, later write an android client ....
Example Design
For simplicity and ease of understanding, this example:
Example flowchart: clientA-> (connect) server
The above is the process of the entire example.
This article is from the "game a chai" blog, please be sure to keep this source http://youxilua.blog.51cto.com/3357586/1186126