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 bother with it. Recently, I suddenly burst into a whim and put mqtt
I read the Protocol once, and there are a lotChineseAll 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 a full-English keyword and finally found mqtt in the search.
If you don't want to read my long article, I suggest you go to the wiki to see 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:
- Lightweight instant transmission protocol
- Designed for low bandwidth or high network costs
- Three service quality levels
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
The version is not provided yet. Later, write an android client ....
Example Design
For simplicity and ease of understanding, this example:
- The server is a broadcast model.
- ForSubscription/ReleaseNo restrictionsTopic(Mainly to prepare for later knowledge)
SubscriberObtained oncePublisherDisconnect messages
Start the server first, and then start mqttclientsub
Example flowchart: clienta-> (CONNECT) Server
- Start Publisher: mqttclientpub
Example flowchart: clientb-> (publish) server-> (pub) clienta and above are the entire process of the example.
Reprinted: http://www.cnblogs.com/youxilua/archive/2013/04/25/3041528.html