MQTT

MQTT stands for Message Queue Telemetry Transport . It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks.

2017-06-05 sbron I created an MQTT client library and published it on chiselapp

2017-08-21 dzach Thanks sbron! The client library works nicely with mosquitto . Maybe it should find its place in tcllib?

Is there a way to figure out if a client is currently connected to an mqtt broker?


gambitcomm - 2017-10-13 14:15:21

I am running a simple subscriber client with this code, eg.

        set client [mqtt new]

        $client connect uwe-client $::addr

        $client subscribe {BCDS/#} message_callback

and while it works with a local broker, eg. mosquitto, it fails with an internet broker, eg. iot.eclipse.org.

The stack trace is

your script1507901608909 (::oo::Obj22_coro): can't read "msgid": no such variable
    while executing
"dict set rc msgid $msgid"
    (class "::mqtt" method "receive" line 35)
    invoked from within
"my receive"
    (class "::mqtt" method "listen" line 27)
    invoked from within
"my listen"

While debugging this problem, it seems that the API cannot handle concatenated messages into one packet. I cannot prove it without much effort. Can someone tell me yes or no whether concatenated messages are handled properly, eg. the packet to the receive method contains a MQTT request and a half?

chw 2018-09-26

See the patch https://www.androwish.org/index.html/info/fcedb208a754d073 which deals with the case of partial MQTT messages in the receive method.

It allows me to watch the German river and sea levels using the mosquitto test broker with this little snippet:

package require mqtt
proc cb {topic content status} {
    puts "$topic: [encoding convertfrom utf-8 $content]"
}
set client [mqtt new]
$client connect test-client test.mosquitto.org 1883
$client subscribe "de.wsv/pegel/#" cb
vwait forever

EF 2018-10-01

I have made available a slightly modified version of sbron's excellent library at github . My version adds the ability to specify the socket command to use for opening connections, thus making it easy to connect to remote facing MQTT servers with TLS. The fix is also proposed as a github at the original project.

In addition, I also have a wrapper with a number of additional features. The wrapper requires toclbox to function. Advantages brought by the wrapper are all explained in the main README , but in short it brings:

  • Automatic reconnection (exponential backoff) to the broker.
  • Client naming interface to benefit from persistent sessions.
  • Message enqueuing during connection losses sessions.
  • Logging

EF 2019-01-30

I use MQTT a lot, since it seems to be the common denominator for most IoT platforms "out there". Below are a few packages worth mentioning here:

  • mqtt2any connects to a server, takes a number of topic subscriptions and is able to execute code in slave and safe interpreters for each data received on the topics.
  • http2mqtt is an HTTP application server that will by default forward any HTTP data posted to a path to the same topic at a remote MQTT server. The server is also able to execute arbitrary code on data arrival, and post to MQTT of your choice. Execution occurs in slave and safe interpreters.
  • mqtt2disque is a module tuned for being part of a slave interpreter in mqtt2any and able to post Jobs into a Disque queue (Disque is a clustering job queue server by antirez).
  • chopper is a module tuned to be used from, for example http2mqtt and aimed at facilitating SenML generation and pushing it to an MQTT server in chunks (probably after data conversion).
  • Docker Mosquitto is an extension of the official Mosquitto Docker image that makes it possible configuration of the server using environment variables, as is tradition with Docker images. The image is also able to properly communicate with brokers using TLS certificates signed by "official" authorities (as in the list in your browser). Configuration of the server using environment variable uses a Tcl script for dividing the main configuration file into more graspable sections.