MQTT Protocol Note Mqtt.io Project WebSocket protocol support

Source: Internet
Author: User
Tags mqtt broker

Objective

The MQTT protocol focuses on network and resource-constrained environments, and it is normal to not consider the Web environment at the beginning of the establishment. Nevertheless, it does not mean that it is not suitable for the HTML5 environment.

HTML5 WebSocket is a two-channel communication based on TCP, similar to TCP communication, and is suitable for web browser environments. Although the MQTT gene plane chooses TCP as the communication channel, we add a codec, and Mqtt over WebSocket is also available.

The benefits of this, the use of MQTT is extended to HTML5, desktop browser, mobile WebApp, hybrid, and so on, a number of imaginary space. In this sense, MQTT will have its own space for both mobile and web-side.

Browser support

In other words, modern browsers already support WebSocket, which has a list of all browser support:

For a more detailed list, please visit directly: http://caniuse.com/websockets

There is no doubt that Firefox and Google Browser are driving the development of modern browsers, as well as support for the HTML5 standard. The browser that supports WebSocket is a 73.88% support rate simply from the above figures. But you actually have to refer to the browser market share:

Data, from: April 2014 Global Mainstream browser market share ranking

More than 60% users on the Machine browser support WebSocket, the data is very impressive.

Mobile hybrid applications will be very welcome websocket
    • Built-in browser supports html5,javascript operation WebSocket connection to Mqtt
    • Connect to the MQTT server with the native TCP socket channel, exposing the JavaScript interface and indirectly using
Do not support WebSocket desktop browser, you can consider the flash socket to help!

For some historical browsers that do not support Websocker, consider a flash socket, although using flashsocket to simulate websocket is easy to understand, but the conditions are as follows:-Requires a separate port for secure cross-domain access policies- Browsers that require a browser to support binary BLOBs support binary operations:

From: HTTP://CANIUSE.COM/XHR2

Compare the support WebSocket and XHR2 desktop browser, overlap rate is very high, use flash socket to simulate websocket is not necessary, in similar to the IE platform, rather than directly using the flash version of the

Https://github.com/yangboz/as3MQTT/tree/master/MQTTClient_AS3

What to do if the WebSocket browser is not supported

Not all browsers support websocket, especially the IE6/IE7/IE8/IE9 that hinder historical development. The MQTT protocol is incompatible with the HTTP plain text of the binary protocol at all, especially if the browser-side JavaScript handles the text very well, but the binary looks like a clumsy hand-corner, unless it supports XHR2.

    1. Using the Flash MQTT Client alone, this is not a deep insight, practice very little, inconvenient to say, if you know, you may wish to inform one or two.
    2. Binary docking with HTTP plain text mode

This section will be devoted to the following.

Server-Side Support

Some of the existing solutions may be behind MQTT broker, preceded by the addition of a layer of proxies. For example: Mod_websocket, corresponding online demonstration: http://test.mosquitto.org/ws.html

The surface looks very decoupling, in fact, the imitation is still the traditional type of short-link reverse proxy architecture: Nginx/apache +java/php/python/ruby.

The client establishes a connection, and the server side needs to use at least two file handles, with one more layer of paths in the middle. Elegant solutions that can be viewed from the Socket.io. A set of server-side programs, while providing several protocols for terminal selection. In fact, an MQTT broker middleware server can bind multiple ports, a 1883 port for pure TCP, a 80/8080 port for WebSocket, and a shared base logic for different protocols.

WebSocket Protocol Adaptation

Server added to WebSocket support, basically not used for much change. Compare TCP to the list of processors attached to a single channel:

1234567891011 public class Tcpchannelinitializer extends channelinitializer< socketchannel> {   @Override public void initchannel (SocketChannel Span class= "Pl-v" >ch) throws exception { channelpipeline pipeline = ch pipeline.addlast ( new Mqttmessagenewencoder (), new mqttmessagenewdecoder (), new mqttmessagehandler ()); }}
View Rawtcpchannelinitializer.java hosted with? by GitHub

WebSocket a list of processors that correspond to a single channel:

123456789101112131415161718192021st22232425262728293031323334 import io.mqtt.handler.HttpRequestHandler; import io.mqtt.handler.MqttMessageHandler; import io.mqtt.handler.coder.MqttMessageWebSocketFrameDecoder; import io.mqtt.handler.coder.MqttMessageWebSocketFrameEncoder; import io.mqtt.handler.http.HttpJsonpTransport; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler; public class Websocketchannelinitializer extends channelinitializer<socketchannel> { Private final static String Websocketuri = "/websocket"; private httprequesthandler httprequesthandler = new httprequesthandler ( Websocketuri);Static {httpjsonptransport Httpjsonptransport = new httpjsonptransport (); Httprequesthandler. Registertransport (Httpjsonptransport); }@Overridepublic void Initchannel (final socketchannel ch) throws Exception { Ch. Pipeline (). AddLast ( new Httpservercodec (), new Mqttmessagewebsocketframeencoder (), new Httpobjectaggregator (65536), Httprequesthandler,new Websocketserverprotocolhandler (Websocketuri), new Mqttmessagewebsocketframedecoder (), new Mqttmessagehandler ()); }}
View Rawwebsocketchannelinitializer.java hosted with? by GitHub

In order to support the WebSocket protocol, only additional additions were made:

123456789 @sharablepublic class Mqttmessagewebsocketframeencoder extends messagetomessageencoder<message> { @Overrideprotected void encode (channelhandlercontext ctx, Message msg, list<object> out ) throws Exception { if (msg = = null) return; byte[] Data = ((Message) msg). tobytes (); Out. Add (new binarywebsocketframe (unpooled. Wrappedbuffer (data))); }}
View Rawmqttmessagewebsocketframeencoder.java hosted with? by GitHub
1234567891011121314151617 public class Mqttmessagewebsocketframedecoder extends messagetomessagedecoder<binarywebsocketframe> { private Mqttmessagenewdecoder messagenewdecoder; public Mqttmessagewebsocketframedecoder () { Messagenewdecoder= new mqttmessagenewdecoder (); }@Overrideprotected void decode (channelhandlercontext ctx, binarywebsocketframe wsframe, list<object> out ) throws Exception { bytebuf buf = wsframe. Content (); this. MessagenewdecoderDecode (CTX, buf, out); }}
View Rawmqttmessagewebsocketframedecoder.java hosted with? by GitHub

Summary

It is easy to talk about a chase websocket, in short, the support of WebSocket. There is time behind to write how to use the HTTP protocol to achieve MQTT over HTTP effect.

Original http://www.blogjava.net/yongboy/archive/2014/05/26/414130.html

MQTT Protocol Note Mqtt.io Project WebSocket 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.