---
title: Paho Lua - MQTT Client Library
description: "Learn how to use the Paho Lua MQTT client library, a text editor-based client ideal for rapid development of MQTT client applications on many platforms."
date: 2015-08-24
published: 2015-08-24
author: Kévin KIN-FOO
tags:
  - MQTT
  - MQTT Client
source: https://www.hivemq.com/blog/mqtt-client-library-encyclopedia-paho-lua
---

# Paho Lua - MQTT Client Library

Paho Lua Client library implements client-side subset of the [MQTT protocol ](/blog/mqtt-essentials-part-1-introducing-mqtt)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.

## Overview of Paho Lua MQTT Client Library&#x20;

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

## Features Supported by Paho Lua MQTT Client Library&#x20;

## Advance Features Supported by Paho Lua MQTT Client Library&#x20;

## Installation of Paho Lua MQTT Client Library&#x20;

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.**

## How to Connect Paho Lua MQTT Client to an MQTT Broker?&#x20;

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](https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/) 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.

## How to Publish an MQTT Message Using Paho Lua MQTT Client Library

Once you are connected it is as simple as:

It publishes a `fancy message` on `topic/sample`.

## How to Subscribe to An MQTT Message Using Paho Lua MQTT Client Library

You can subscribe on several [topics](/blog/mqtt-essentials-part-5-mqtt-topics-best-practices) by providing them to `client:subscribe()` in a table. Once something arrives on topic, callback defined at connection is called.

## How to Unsubscribe to An MQTT Message Using Paho Lua MQTT Client Library

You no longer want to get message from a topic? Put it in a table and give it to

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

## How to Disconnect From an MQTT Broker While Using Paho Lua?

Quite self-explanatory.

## Full Example Application of Using Paho Lua MQTT Client Library

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/)

Check out[ HiveMQ MQTT Client libraries and other featured MQTT client libraries](/mqtt/mqtt-client-library-encyclopedia)[.](https://www.hivemq.com/mqtt/mqtt-client-library-encyclopedia)
