Install Mosquitto MQTT Broker on Windows and Linux

In this tutorial, we will learn to install Mosquitto MQTT broker on Windows machine as well as Linux Ubuntu operating system. An MQTT broker is the main component of an MQTT protocol. It receives MQTT messages from all MQTT clients and forwards them to all clients which subscribed to those topics.

Install Mosquitto MQTT broker on Windows and Linux

In this tutorial, we will focus on installing Mosquitto broker on Windows and Linux only. However, it is recommended to install Mosquito MQTT broker on Raspberry Pi if you want to use it for your projects. This is because if you have installed the broker on Windows/Linux, you need to keep your machine up and running all the time in order for IoT devices to remain connected with the broker.

Refer to the following guide, if you want to install the Mosquitto Broker on Raspberry Pi instead:

What is MQTT

MQTT is a lightweight messaging protocol for IoT device communication.

  • MQTT is known as Message Queuing Telemetry Transport protocol.
  • It is a lightweight messaging protocol and helps resource-constrained network clients with a simple communication mechanism.
  • Unlike, most messaging systems, we don’t have to assign addresses to MQTT clients.
  • MQTT uses simple publish/subscribe communication-based on a topic.
  • This protocol runs on top of TCP / IP in order to provide reliable data delivery.
MQTT Protocol Introdution

In order to understand MQTT protocol, you should grasp these four main components components

MQTT Publisher/Subscriber

The first main concept in MQTT is a Publisher/ Subscriber system.  A publisher device publishes a message on a specific topic and a Subscriber device subscribes to the topic which will be sent by a publisher. On top of that, more than one device can subscribe to the topic.

MQTT Publisher Subscriber Example

For example, according to above figure:

  • Device1 is a publisher and Devices(2-3) are subscribers.
  • Let’s suppose, device1 publishes on a topic “ESP32/Temperature” and Devices(2-3) subscribe to the same topic.
  • Hence, Devices(2-3) are receivers and Device1 is a sender.

Installing Mosquitto Broker on Windows

Lets start with installing Mosquitto broker on our Windows machine. Follow the guide step by step for a successful installation.

We will start by downloading the Mosquitto Broker for MQTT protocol by Eclipse. After that we will install that in our Windows machine. First of all open a new webpage, type Mosquitto and open the download page of the official website. The following link: https://mosquitto.org/download/ also takes you to the download page.

Head over to the Windows section, and click the .exe file according to your Windows system type to download. Our Windows machine has an operating system of 64 bit hence we will download the .exe file according to that.

Download Mosquitto Broker on Windows

The application file will start downloading. After the download is finished, click the file to open it and begin the installation process. Click ‘Next’ to begin the process.

Install Mosquitto Broker on Windows 1

Next we will be asked to choose the features of the Mosquitto Broker. By default, both ‘Visual Studio Runtime’ and ‘Service’ were ticked so we leave it as default and click ‘Next’ to move ahead.

Install Mosquitto Broker on Windows 2

The next step is to choose the installation location. You can specify your own preferred destination folder. We are going with the default path already set.

Now click the ‘Install’ button and the installation will start.

Install Mosquitto Broker on Windows 3

After a few moments, the installation will successfully complete. Click the Finish button to close the setup.

Start, Stop and Restart Mosquitto Broker Service

Now let us test our Mosquitto Broker service. Open the Task Manager on your system and head over to the Services tab. Here find the mosquitto service and check its status. Here, we can view that it is Running.

Mosquitto Broker Task Manager

Right click on the Mosquitto service, and you will have various options which you can select. You can stop the service by clicking ‘Stop’ and even restart it by clicking ‘Restart.’ Other options are also available which allow you to open more details.

Mosquitto Broker Task Manager 2

Alternative Method to Start, Stop and Set Port

We can even use command prompt in Administrative mode to run the Mosquitto Broker service. First of all, open command window as an administrator.

Command Prompt to test MQTT broker 1

Then navigate to the path where you installed the Mosquitto Broker application. In our case, we set the default path which was “C:\Program Files\mosquitto

Command Prompt to test MQTT broker 2

To start the Mosquitto service, type the command: “net start mosquitto

Command Prompt to test MQTT broker 3

If you want to stop the service, then type the command: “net stop mosquitto

Command Prompt to test MQTT broker 4

In order to change the port for the Mosquitto broker, type the following command: “mosquitto -p 1883

This command will set the default port as 1883.

Command Prompt to test MQTT broker 5

Testing Mosquitto Server on Windows

To test or Mosquitto server, open two command prompts as an administrator and navigate to the directory where we installed the mosquitto application.

One command prompt will act as a publisher, that will publish an MQTT message to the test/topic. The other command prompt will act as a subscriber, that will subscribe to the test/topic and display the message received in the command shell.

To start the command line subscriber, we use the following command.

mosquitto_sub -v -t 'NAME_OF_TOPIC'

Here the name of our topic is test/topic. This command line will therefore subscribe to the topic: test/topic.

Type the following command in the first command prompt:

Testing Mosquitto Broker on Windows Subscriber

To publish an MQTT message with the command line publisher, we use the following command.

mosquitto_pub -t 'NAME_OF_TOPIC' -m 'MESSAGE_TO_BE_PUBLISHED'

Here the MQTT message that we want to publish is WELCOME!. This command line will therefore publish ‘WELCOME!’ to the topic: test/topic.

Type the following command in the second command prompt:

Testing Mosquitto Broker on Windows Publisher

Now the subscriber terminal will display the message it received from the test/topic.

Testing Mosquitto Broker on Windows Subscriber demo

Hence, the Mosquitto Broker is working fine as one client was able to publish a message to a topic and another client was able to subscribe to that topic and receive the message.

Installing Mosquitto Broker on Ubuntu Linux

In this section, we will install Mosquitto broker on Ubuntu machine and test it as well. As with the installation of any other package, Mosquitto is also available in the Ubuntu repositories.

To install Mosquitto Broker, open the Terminal in your Ubuntu machine and type the following command:

sudo apt-get install mosquitto

After typing the correct password for the user, the installation process starts.

Install Mosquitto Broker on Ubuntu 1

Type ‘Y’ to continue and the installation completes shortly.

Install Mosquitto Broker on Ubuntu 2

After the installation is complete, the Mosquitto Broker will automatically start. To view the current processes running within our system we will type the following command in the shell:

ps -e

As a result, you may notice that mosquitto is part of the processes currently running on the system.

Install Mosquitto Broker on Ubuntu 3

Testing Mosquitto Broker on Ubuntu

Let us show you how to subscribe and publish messages to a topic. Before that we will have to install a Mosquitto client. To do that, type the following command in the terminal:

sudo apt-get install mosquitto-clients
Testing Mosquitto Broker on Ubuntu installing Mosquitto Client

This will install the Mosquitto client on our machine.

Next, to subscribe to topic for e.g. ‘test/topic’ type the following command:

mosquitto_sub -h localhost -t "test/topic"
Testing Mosquitto Broker on Ubuntu Subscriber

Open another terminal and publish the message ‘WELCOME!’ on the topic: test/topic. For that, type the following command in the second terminal:

mosquitto_pub -h localhost -t "test/topic" -m "WELCOME!"
Testing Mosquitto Broker on Ubuntu Publisher

Right after we publish the message to the topic test/topic, the subscriber receives it and displays it in its terminal.

Testing Mosquitto Broker on Ubuntu Subscriber demo

Likewise, we publish another message ‘Hello World’ on the topic and the subscriber receives it accordingly.

Testing Mosquitto Broker on Windows Publisher and Subscriber demo

Now if you want to create MQTT based projects with Mosquitto broker and ESP32, you can read these projects:

Leave a Comment