Mqtt tossing notes-simple protocol reading

Source: Internet
Author: User
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:

  1. Lightweight instant transmission protocol
  2. Designed for low bandwidth or high network costs
  3. 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.

  • The XMPP message body uses XML

    |--------------------| | <stream> | |--------------------| | <presence> | | <show/> | | </presence> | |--------------------| | <message to='foo'> | | <body/> | | </message> | |--------------------| | <iq to='bar'>
    | | <query/> | | </iq> | |--------------------| | ... | |--------------------| | </stream> | |--------------------|

  • HTTP

    HTTP-message = request | response; http/1.1 messages

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:

  • Nodejs: mqtt. js

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:

  1. The server is a broadcast model.
  2. ForSubscription/ReleaseNo restrictionsTopic(Mainly to prepare for later knowledge)
  3. SubscriberObtained oncePublisherDisconnect messages

  4. Start the server first, and then start mqttclientsub

Example flowchart: clienta-> (CONNECT) Server

  1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.