11 Sep 2015

MQTT with Mosquitto: A beginner's tutorial

Using MQTT on Mosquitto and Paho

In the current tutorial, I shall describe the usage of one of the most widely used protocols named “MQTT” using open source server/broker and clients. I have used my Windows 7 machine for running the tutorial.


MQTT( Message Queue Telemetry Transport) is used for device data collection. As its name suggests, its main purpose is telemetry, or remote monitoring. Its goal is to collect data from many devices and transport that data to the IT infrastructure. It targets large networks of small devices that need to be monitored or controlled from the cloud. MQTT uses a hub-and-spoke architecture, where all the devices connect to a data concentrator server. The protocol works on top of TCP, which provides a simple, reliable stream of data. MQTT works in “publish subscribe” mode and is a very good means of low power transfer of data from devices to broker with reliability. The messages' payloads are just a sequence of bytes, up to 256MB, with a fixed header of two bytes to most messages. The clients can subscribe to these messages and get updated by the broker when new messages arrive. MQTT lets clients and brokers set a "Quality of Service" on messages basis from "fire and forget" to "confirmed delivery".
Please refer to <http://mqtt.org/faq> for more information on the specific protocol.


MQTT needs client implementations. The Eclipse Paho project is part of the Eclipse Foundation's M2M (Machine to Machine) mission to provide high quality implementations of M2M libraries and tools. Under the Paho banner, open source client libraries for MQTT are being developed.  MQTT C and Java libraries with Lua, Python, C++ and JavaScript are at various stages of development. In this article we'll be showing how to use the Paho Java MQTT libraries to publish and subscribe.


In order to receive published messages and send them on to any clients who have subscribed, we need a broker. Mosquitto is one of such brokers which is easy to configure and run for MQTT messages. Mosquitto is open source, so you can download it and run it on your own system, on different operating systems like Windows, Mac OS X, Linux or many other platforms. The Mosquitto broker code is also being contributed to Eclipse as part of a new project.

Mosquitto's default configuration means it is set up to not use username/password authentication and it accepts all connections on port 1883. It also comes with two clients, mosquitto_pub and mosquitto_sub. mosquitto_pub client is used for publishing simple messages, while the later is for subscribing to a topic and printing the message that it received. It is also widely used for debugging applications. We shall see the usage of both these clients in the tutorial.

We shall test out the following configuration using mosquitto and the code snippets.

Downloads required

You need to download the following.

Eclipse IDE for Java developers: Source <https://eclipse.org/downloads/>, select the 32 bit or 64 bit version as compatible to your system.

Install eclipse as per the guidelines. Make sure a version of JRE is installed in your machine and eclipse should point to the same JRE folder for execution

Mosquitto: Source <http://mosquitto.org/download/> . I have used the binary installation as follows.

mosquitto-1.4.3-install-win32.exe (~200 kB) (Native build, Windows Vista and up)

At the time of installation, you will be prompted to install additional dlls (for win 32) as follows

libeay32.dll, ssleay32.dll  and  pthreadVC2.dll

You need to get the dlls from the URLs specified during download process (on clicking the links, you will be taken to the sources of the dlls directly). You need to copy the dlls is the directory where mosquito is installed. These dlls are mandatory for running mosquito.

Add the path of the folder where Mosquitto executables are copied using the System Properties-> Environment variables (on Windows 7). This will make sure that mosquito executable will run from any place. 

Paho : Source : <http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/

Download the following and extract in a folder (can be under eclipse). I used the “zip” version.

After extraction, add the path of the downloaded files in your CLASSPATH through System Properties-> Environment variables (on Windows 7). Specifically, the following folder should be added to the CLASSPATH for the current exercise.


Running the application

On the Eclipse IDE

Open the Eclipse IDE, create a new Java project and then select Configure Convert to Maven project.

Since the Paho MQTT code isn't in Maven Central, we need to include its repository. This needs to be done on the pom.xml file from Eclipse IDE.

Open the pom.xml file and after </version> add

<name>Paho MQTT Client</name>

Then we need to add the dependency for the Mqtt-client code. On the pom.xml file, after </build>, add


From Command Prompt

Run Mosquitto.exe (type “mosquitto” from command prompt). One screen will open up

To make sure (or check) whether mosquitto  is running, you can open another command prompt window and run “netstat –an”.  You should be able to see the following screen with boxed line (showing Listening at port 1883).

Now we have made sure that MQTT broker is running and is listening to port 1883.
Open another command prompt and run the following command. With the options used, the messages are dumped on the screen.

mosquitto_sub -t "#" –v

Go back to Eclipse IDE and create a new class called PahoDemo. Add the following lines of code.

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class PahoDemo {

         MqttClient client;
         public PahoDemo() {}

         public static void main(String[] args) {
           new PahoDemo().doDemo();

         public void doDemo() {
           try {
             client = new MqttClient("tcp://localhost:1883", "pahomqttpublish1");
             MqttMessage message = new MqttMessage();
             message.setPayload("A single message from first program".getBytes());
             client.publish("pahodemo/test", message);
           } catch (MqttException e) {


You will be able to see the message “pahodemo/test A single message from first program” on the mosquitto_sub screen.

Also if you open another command prompt and type the following.
mosquitto_pub -t "mosquittodemo/test" -m "Hello"

You will get the same message echoed at the mosquitto_sub screen as follows.

Thus, we managed to get a basic Paho MQTT client running. The messages are displayed both from the Java Application as well as the mosquitto_pub client


  1. -Sinh ra là thôn hồn, du hồn ta xúc phạm đến ngươi thì trừng phạt cũng

    chẳng có gì phải nói. Nhưng cắn nuốt không gian sụp đổ, là sứ mệnh mà ta

    chờ đợi từ khi được sinh ra. Mặc dù chúng ta không cắn nuốt, thì cũng
    dịch vụ làm báo cáo tài chính

    dịch vụ kế toán thuế
    eco green city
    chung cư goldmark city
    công ty làm dịch vụ kế toán
    chung cư 89 phùng hưng
    tiếng anh cho trẻ em
    chung cư newskyline
    hateco hoàng mai
    chung cư hà nội
    dịch vụ hoàn thuế
    dịch vụ kế toán thuế

    có những thôn hồn khác đến đây cắn nuốt mà thôi.

    Khi ba đạo thần thức khổng lồ kia mỗi người nói ra một câu, cũng không

    tiếp tục cắn nuốt, mà yên lặng đợi Vương Lâm trả lời. Trong ý nghĩ của

    bọn họ, Vương Lâm dĩ nhiên cũng là tồn tại ngang bằng với bọn họ.

    Đây là lần đầu tiên Vương Lâm nghe được thôn hồn nói đến, hắn trầm tư

    một chút, trong đầu sắp xếp lại những ký ức của Mã Lương khi còn sống.

    Biết được truyền tống trận bắt đầu mở ra, nếu như lúc này chiến trường

    ngoại vực hoàn toàn sụp đổ, sợ rằng truyền tống trận này, có thể mở ra

    được hay không cũng còn chưa biết được.

    -Ta cũng không phải muốn ngăn cản, mà hi vọng thời gian sụp đổ cuối cùng

    của cái chiến trường ngoại vực này, có thể trì hoãn một chút được