MQTT Client Library Encyclopedia – Paho Lua

Guest post by Kévin KIN-FOO

M2Mqtt
LanguageLua
Websitehttps://www.eclipse.org/paho/
API-StyleAsynchronous
LicenseEPL

Description

Paho Lua Client library implements client-side subset of the MQTT protocol specification 3.1. An advantage of using Lua is that only a text editor is required for rapid development of simple MQTT client applications on many platforms. Cross compilation issues are avoided.

Initially part of Aiko Platform, this library is now part of Eclipse Paho project.

Features

MQTT 3.1
MQTT 3.1.1nok
LWTnok
SSL/TLSnok
Automatic Reconnectnok
QoS 0ok
QoS 1nok
QoS 2nok
Authenticationok
Throttlingnok

Usage

Installation

As I am writing easiest way to install Paho Lua Client is from sources using LuaRocks.

You are all set.

Note: tested on Lua 5.1.

Connect

This section shows the API usage how to connect with the library to a MQTT broker.

First step is creating MQTT client, here test.mosquitto.org is used as broker host. As port is not specified, 1183 is used by default.
Then you can simply call client:connect() with a client identifier of your choice.

Connect with Username / Password

Authentication is just one additional line.

Calling client:auth() before client:connect() enables to set username and password.

Publish

Once you are connected it is as simple as:

It publishes a fancy message on topic/sample.

Subscribe

You can subscribe on several topics by providing them to client:subscribe() in a table.
Once something arrives on topic, callback defined at connection is called.

Unsubscribe

You no longer want to get message from a topic? Put it in a table and give it to client:unsubscribe().

Disconnect

Quite self-explanatory.

Full Example Application

What we are doing here is publishing random strings on topic/sample forever until being told to “stop”.

First we connect to broker.mqttdashboard.com on default port 1883. At connection, we define a callback to handle received messages.

Then we subscribe to topic/sample topic.

Now we publish a string on topic we just subscribed to, client:handler() checks for received messages. When there is one, callback(topic, message) is called to deal with it. We repeat this relentlessly.

Once this message is string “stop”, we stop looping, unsubscribe from topic/sample.

To clean all this, we disconnect from server and destroy our client to free allocated memory.

You can also find this example code at https://github.com/KINFOO/org.eclipse.paho.lua.sample

Author Information

Kévin KIN-FOO | Sierra Wireless
I am doing fine things at Sierra Wireless. I love my software simple.
   Website

2 comments

  1. Benjamin Cabé says:

    Great post!
    I think the Lua client supports LWT so you may want to update the table at the beginning!

    1. Hi Benjamin,

      thanks for the heads up! We updated the table to reflect your suggestion.

      All the best,
      Dominik from the HiveMQ Team

Leave a Reply

Your email address will not be published. Required fields are marked *