Summary of MQTT

Note

MQTT As a Service: sangoをリリースしました

In August 2014, we are beginning to use the MQTT and can be easily registered in the GitHub account sango the 時雨堂 is Released.

There is also a free plan, so if you want to use MQTT once, we recommend using sango.

Recently voluntas Mr. 活動 to you is, we have suddenly MQTT related hot. I think that it is probably because the observation range is narrow.

However, there are buzzwords such as M2M (Machine to Machine) and IoT (Internet of Things), and it is becoming an era when things are connected to the Internet, and I feel that the value of MQTT is getting higher. It may also be drawing attention in the meaning of a protocol suitable for the mobile age.

I will summarize here about MQTT once.

What is MQTT

MQTT (MQ Telemetry Transport) is a lightweight message protocol based on the publish / subscribe model. It is characterized by the function to operate in a place where the network is unstable and the weight saving for moving with a non powerful device.

It comes to MQ and the name, but in the so-called Job Queue for dispersing the load ありません . Please use AMQP etc. for such use.

MQTT is specialized in delivering messages, especially for applications that have to have many Publishers / Subscribers. In addition, it is also used for applications that need real-time communication, taking advantage of lightweight points and synchronous communication points.

In particular

And so on.

history

Originally MQTT was formulated by IBM and Eurotech since 1999. IBM is IBM MessageSight have called products.

Then in 2011 Eclipse FoundationにMQTTのコードが寄贈され , also, in 2013 OASISという国際標準化団体にて標準化 is now to be done.

Specification is a royalty-free 公開 have been. Current specification 3.1 is the latest, so 3.1.1 will appear soon.

Characteristic

I will briefly summarize the features of MQTT.

  • Less fixed overhead of 2 bytes and less processing
  • One-to-many, many-to-many message distribution, and flexible by Topic wildcard
  • Three types of QoS can be specified according to application characteristics
    • Three types of "one at a time", "at least once", "exactly once"
  • "Durable subscribe" which can receive disconnected messages after disconnection occurs and reconnected
  • "Last Will and Testament" which sends a message decided in advance when the Publish side disconnects
  • Save the last message so that it can be sent if you subscribe later "Retain"

QoS, Will etc are characteristic features of MQTT.

Note that MQTT operates on TCP / IP, and payload is not specially specified in the specifications. It seems that strings are often used, and there are also stories about using MessagePack.

Let's explain these.

lightweight

MQTT has a fixed length header with minimum 2 bytes, low overhead, and the protocol is simple. Therefore, it is superior to HTTP over network bandwidth and processing speed. Also, due to less processing, power consumption has also decreased, making it suitable for mobile devices.

IBMの資料 According to the, compared to HTTP

  • Traffic from one tenth to one hundredth, ie 10 to 100 times throughput
  • Battery consumption is less than 1/10

That is.

Besides, it is a connection oriented protocol that sends heart beat at fixed intervals instead of connectionless like HTTP, and performs bidirectional communication. Therefore, high immediacy communication is possible.

But is a performance, 別のIBMの資料 In

  • Linux, 4 x 4-core 2.93 GHz Intel Xeon with 32 GB of RAM and 10 Gbit LAN

so

  • 100,000 clients
  • 13,000 messages / sec

To

  • 25% CPU

It seems that he decided it. Although it is the lightest case (QoS 0), it seems that 6000 messages / sec was flagged with 30% CPU load even in the heaviest case (QoS 2).

Topic

Each message has "Topic". Topic is

/r_rudi/second_house/room10/light/watt

And so on / has a hierarchical structure of the separator.

On the Subscriber side, you specify the Topic you want to subscribe to. At this time you can specify a Wildcard, not an exact match.

For example, it can be specified as follows.

/ R_rudi / second_house / room 10 / light / watt
Specify exact match
/ R_rudi / second_house / #
/ R_rudi / second_house / all below topic
/ R_rudi / second_house / + / light / watt
Broad match (in this case you get light watt of all rooms)

Of course one subscriber can subscribe more than one topic.

QoS

In MQTT, QoS can be set for message delivery.

  • 0 QoS : At Most Once - most once. I do not guarantee that it will arrive.
  • 1 QoS : At Least Once - at least once. There is a possibility of overlapping.
  • 2 QoS : Exactly Once - exactly once

Since this can be changed for each message to be sent, normally you can send it with QoS 0 even though it does not arrive, but you can set QoS 1 or 2 so that the control message always arrives.

For QoS 1 and 2, ACK and retransmission etc are also defined in the protocol specification.

Durable subscribe

MQTT assumes a situation where the network is unstable. Therefore, in the case of QoS 1 and QoS 2, the server (Broker) keeps the sent message for a certain period.

If the Subscriber disconnects suddenly without explicit DISCONNECT or UNSUBSCRIBE, there is a function to retransmit the message between them when reconnecting.

This makes it possible to operate on unstable networks and servers.

Will

The client can add information called Will when first CONNECTing the server. When the server can not communicate with that client, it sends the topic and payload specified by this Will.

By doing this, you can judge that the Publish side is dead on Subscriber side.

The death check is judged by answering ping sent at regular intervals or not. Since this interval is also decided for each connection, you can do something like this device because you are worried about batteries so you can increase the interval.

Retain

The Retain function is a function that the MQTT server holds the message that was last published, and passes the message to a new Subscriber.

MQTT is a Publish / Subscribe model. Therefore, messages will only be sent to clients that subscribed when they were published. Therefore, for example, even if you subscribe to get updated information every hour, you will not get any information for up to an hour.

However, even in this case you can get the latest information using the Retain function.

Security

MQTT allows you to specify a user name and password the first time you connect to the server (Broker). However, since the password itself is sent in clear text, security is to be established through SSL.

Also, there is no Subscriber's certification, so it is not in the specification that this Topic will only deliver to this Subscriber. However, Mosquitto, which is described later, implements a function that allows access management of Read and Write for each user name.

I wrote about specifying the user name and password before, but this handling is implementation dependent. In addition to normal authentication, it is also possible to implement authentication using LDAP or OAuth.

Implementation

Since the protocol is simple, there are many implementations. However, there are not many things that are properly implemented, such as QoS 0 alone.

Among them, we list enumerated items.

Mosquitto is an old implementation written in C and implements almost all functions and is often treated as a reference implementation.

RabbitMQ is a well-established MQ server made by erlang. It also supports AMQP, but it also supports MQTT.

Paho is an implementation donated by IBM as mentioned above, and it supports various languages such as Java version, Python, JS etc.

ActiveMQ Apollo can also make other STOMP, AMQP, etc. of MQTTT.

Also, it is closed source, but there is also one called HiveMQ.

Note

時雨堂 You have been made the MQTT Broker. It is implemented in erlang language, it seems to have considerable speed and fault tolerance. Unfortunately closed source, such as the spec and development log こちら can be referenced from.

WebSocket

WebSocket is a protocol to carry out two-way communication over HTTP, and alternative operation if the recent browser.

Since this protocol provides the same function as MQTT bidirectional communication, there is also a library that connects to the MQTT server using WebSocket on the browser.

Summary

Recently I summarized about MQTT that it seems slightly slightly hot slightly but maybe not.

Light protocols, connection oriented, disconnection detection, retransmission functions, etc. are various protocols that can be used. It may fit a lot of usages unexpectedly.

It is OK to do "do everything with HTTP", but it may be one idea to try using other protocols.

By the way, the MQTT related software I wrote is the following three. Everything is at the concept level, but as long as it becomes a reference for something.