MQTT Protocol Note Mqtt.io Project HTTP protocol support

Source: Internet
Author: User

Objective

When the Mqtt protocol was born, it was never considered for transmission over HTTP. This is also normal, network constrained, unstable network is not suitable for HTTP (2g/3g network everyone using WAP is not OK). On the desktop side of the network more abundant, although the pure text contrast binary is not much advantage, subject to historical legacy and usage habits, as well as a large number of traditional infrastructure to facilitate control matters, traditional Internet/enterprise applications, the HTTP protocol is the default best choice, safe and controllable, man-machine friendly. The choice of HTTP is also understandable.

Although the desktop is declining, it is also a trend to do a unified platform-based message system/message middleware.

MQTT over HTTP, which provides HTTP channel protocol support for the Web environment, is justified in the context of unified platform. Mqtt compared to other HTTP-based switching protocols (such as Socket.io), the data is saved and the message quality is controlled.

In a word, stronger than it, without its light weight. Lighter than it is, without it controllable.

In short, in a resource-constrained environment where performance is so excellent, the desktop will perform better.

HTTP Support conditions

Binary support, as far as browser-side JavaScript is concerned, is very hard to deal with, as in the Stone Age, dealing with some of the industrial era's communication methods. Browsers that support JavaScript binary operations:

From: HTTP://CANIUSE.COM/XHR2

This is basically overlapping with browsers that support WebSocket.

To allow HTTP transport of MQTT-specific message binaries, which are then parsed by browser JavaScript scripts, do not support all common browsers and need to consider transmission in plain text mode.

Design Summary

Ajax way, support across domains, support all major platforms, Desktop + mobile devices, all browsers, mobile, desktop, including ie6+. Then the ideal way is JSONP, based on the long-polling JSONP mode of text communication.

MQTT protocol Key interaction point: 1. Connection gets authorization 1. Subscribe/unsubscribe to topic 1. Post message 1. Wait for subscription message 1. Close connection

If the above functions/steps are supported by HTTP, the server side needs to support receiving HTTP plain text content request, assemble, convert to Java object, realize the data flow and exchange in the business level, and insert into the more specific business directly, so it is easy. This is similar to bridging, but reduces the bridging path (http-MQTT), reducing resource usage and ensuring performance.

HTTP text mode, and Mqtt binary requires some rules of conversion, in order to be compatible, you need to define the interface transport interface separately, Channelentry, dual channel and single channel processing data in different ways, single-channel HTTP JSONP needs to support a short cache message and wait for the client to get it in turn. When you publish/receive a subscription message, the Tcp/websocket uses a dual channel corresponding channel to send directly.

HTTP channels have been set aside to support other types of HTTP transport channels, such as the need to implement a regular long polling/http streaming in a non-browser environment, just need to implement the corresponding interface.

The way the client ID is generated is typically determined by the SessionID generated by the server side. The transfer of plain text is more appropriate than a structured JSON structure.

JSONP only supports HTTP GET mode, which you need to keep in mind.

Transport Interface Definition
    1. Connect to get authorization/jsonp/connect
    2. Subscribe to the topic/jsonp/subscribe
    3. Waiting for subscription message/jsonp/waiting
    4. Release Topic/jsonp/publish
    5. Unsubscribe Theme/jsonp/unsubscrible
    6. Close Connection/jsonp/disconnect

The message type required to be returned is a JSON string, and the subscribed/published message must contain {id:10, msg: ' Specific message content '} like a JSON string.

Simple demonstration

The general transmission is the text content, but has the structure, the non-JSON mo belongs. JSON is a good choice for structured data, whether it's a TCP binary stream or a JSONP-transferred content body.

Browser-side jquery, which supports JSONP requests, here is a simple demonstration:

123456789101112131415161718192021st2223242526272829303132333435363738394041424344454647484950-------------- <! DOCTYPE html><html><head><Meta charset="UTF-8" > <title>jsonp example</title> <script src="Http://libs.baidu.com/jquery/1.5.0/jquery.js" ></script> <script type="Text/javascript" > var clientId = ""; var urlprefix = "Http://127.0.0.1:8080/jsonp"; $ (document). Ready (function () { Doconnect (); }); function doconnect () { Jreq (urlprefix + '/connect', ', function (data) { if (data. Status) { log ("connected!"); clientId = data.clientid; dosub ();}else{ Alert ("Failure!"); } }); } function dosub () { Jreq (urlprefix + '/subscribe', ' topic=sub-msg/webclient1', function (data) { if (data. Status) { log ("subscribed!"); dowaiting ();}else{ log ("bad!"); } }); } function dowaiting () { Jreq (urlprefix + '/waiting', ', function (data) { log ("Got message:id = " + data. ) ID + "msg = " + data.msg); dowaiting (); }); } function jreq (URL, parameter, fn) { $.ajax ({URL: URL, dataType:"Jsonp", jsonp:"Jsonpcallback", data: parameter, Timeout: 300000, Async:true, Success:function (data) { fn (data); } }); } function log (val) { $ ("#log"). html (val + "<br/>" + $ ("#log"). html ()); } </script> </head><body><Div id="Log" ></div> </body></html>
View rawjsonp.html hosted with? by GitHub

Fill in the address, automatically perform the connection, subscribe, receive the message, one of the simplest demo shows its flow.

Summary

Simple implementation of the Jsonp form of mqtt over HTTP, so that the text and binary exchange data between each other. In short, using the MQTT protocol in a pure HTTP environment is a good choice.

Original http://www.blogjava.net/yongboy/archive/2014/06/06/414474.html

MQTT Protocol Note Mqtt.io Project HTTP protocol support

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.