How To Set Up Your Own MQTT Broker on Azure?

An MQTT broker primary responsibility is to manage all messages, filter them, analyze possible clients, and then manage all interested parties and broadcast their messages to all subscribing customers.

MQTT stands for Message Queuing Telemetry Transport. MQTT is a simple posting and subscribing framework that allows you to act like a customer and send and receive messages.

MQTT is an open OASIS platform that provides a standardized messaging mechanism. For limited devices with minimal bandwidth, it’s a publish-subscribe network communication protocol designed for ultra-lightweight, low-power devices. Sensor-based nodes and message systems may be read and managed using MQTT, as well as outputs can be controlled by MQTT instructions.

Today, MQTT is expanding at an exponential rate across a wide range of industries, including automotive, manufacturing, telecommunications, oil and gas, and retail.

Eclipse Mosquito: Why?

As a starting point, let’s talk about what Eclipse Mosquitto is. The Eclipse Mosquitto is an open-source message broker licensed under the EPL/EDL license and supporting the protocol versions 5.0, 3.1.1, and 3.1. Multiple devices, low-power single-board computers, and entire servers can all run on this interoperable, scalable architecture.

Mosquitto also includes a set of C library functions for implementing MQTT clients and servers.

For individual customers, the command lines mosquitto pub and mosquitto sub are very popular and effective.

Setup Requirements for an MQTT broker

Prior to configuring an MQTT broker, gather the required resources.

  • Account for the Docker Hub
  • In a temporary machine, run Docker Desktop (to produce a Docker image — a one-time activity)
  • An instance of Azure Container
  • Account for Azure storage
  • File Sharing in Azure (Inside of Azure storage account).
  • CLI for Azure (Cloud shell)
  • Azure Resource Group

Procedure for Configuring an MQTT broker on Azure

We’ll need a few basic components to build up an MQTT broker on Azure:

  • Image of a Mosquito Broker
  • To mount the container, you must first create volumes
  • Creating a container instance in Azure

Image of a Mosquito Broker

The first thing you’ll need is the “eclipse-mosquito” docker image from the mosquito broker, which you can find on the docker hub (https://hub.docker.com/ /eclipse-mosquitto).

You must have Docker already installed on your machine before you can construct a docker image from it. Download and install Docker from the link below if it isn’t already installed.

https://www.docker.com/products/docker-desktop

Installing eclipse-mosquito is as easy as typing “docker pull eclipse-mosquito” in your command prompt and it will automatically download the image to your computer.

Log in to your Docker Hub account using the “docker login” command in command prompt or Docker desktop; if you don’t already have an account, you may create one at https://hub.docker.com/.

Use the command “docker tag eclipse-mosquito pca31/testsystem1” to tag the “eclipse-mosquito” image as “your-docker-account-id>/docker-image-name>,” in this case “pca31/testsystem1.”

The command “docker image ls” can be used to view newly produced pictures.

Now use the command “docker push your-image-name>” to publish this image to Docker Hub; in this case, we used “docker push pca31/testsystem1.”

This image is now visible in your Docker Hub account.

We can now use this image to make a container.

To Mount the Container, You Must First Create Volumes

  • We can mount three directories on mosquito containers, as specified in eclipse-description mosquito’s (https://hub.docker.com/ /eclipse-mosquitto), but we’ll only use mosquito/config for our configuration.
  • We’ll add the essential files inside the azure file share, such as the mosquitto configuration file (mosquitto.conf), authentication and authorization files (password.txt, roles.txt), and SSL/TLS configuration files (RootCA.crt, server.crt, server.key), and mount it on our azure container instance.
  • If you don’t have an Azure storage account yet, you can create one. You can also make use of your current storage accounts. On the Azure site, go to the Storage accounts section and click “Add.”
  • Click the “Review+Create” button after selecting the resource group and giving the storage account a name like “testsystem1”.
  • After you’ve double-checked everything, click the “Create” button.
  • The “Go to the resource” section will appear once the deployment is complete. Click on it to access all of the available resources.
  • To create a new file share under our storage account, select “File shares.”
  • Then click the “Create” button after giving the file share a name and a quota.
  • Select the MQTT file share by clicking on it.
  • To upload the MQTT configuration files, click the upload button.
  • Tap the file icon, choose the files from your computer’s hard drive, and then click “Upload”.
  • You can now go to the account storage screen and pick “Access keys” from the drop-down menu.
  • We’ll need one of the keys to create the Azure container instance, so copy one of them.

Creating a Container Instance in Azure

Using the Azure CLI, we’ll now build an Azure container instance using our docker image “pca31/testsystem1.”

By selecting the cloud shell icon, you may access the Cloud shell Options.

You can now use specific instructions to execute the following commands.

az container create
--resource-group $YOUR_RESOURCE_GROUP_NAME

--name $CONTAINER_NAME

--dns-name-label $DNS_NAME

--image $DOCKER_IMAGE_PATH

--ports 8883

--azure-file-volume-account-name $AZURE_STORAGE_ACCOUNT_NAME

--azure-file-volume-account-key $AZURE_STORAGE_ACCOUNT_KEY

--azure-file-volume-share-name $FILE_SHARE_NAME

--azure-file-volume-mount-path /mosquitto/config/

The sample command we used for our Dev MQTT broker setup is listed below (Take it as a reference)

az container create

--resource-group Test_Test_Lab-BOServer-251818

--name testsystem1

--dns-name-label testsystem1

--image pca31/testsystem1: latest --ports 8883

--azure-file-volume-account-name testsystem1

--azure-file-volume-account-key

I2S6Z3AluZSyQjqePqA+UgpVlG10qDqfGOql0cuF0p130TdR7KhvvPwspfFlnwusFNg0N5

bGMdas3NNrf9xLOw==

--azure-file-volume-share-name mqtt

--azure-file-volume-mount-path /mosquitto/config/

The script above will start an Azure instance. To see this resource, navigate to the Azure container instance.

In your MQTT client application, use this DNS domain as an MQTT server.

To see the events, properties, and logs of our container instance, go to the Container tab.

Connect to MQTT Broker

It is necessary to collect the following items from Sample Client in order to establish a connection with the MQTT broker.

  • URL of an MQTT broker.
  • The file containing a certificate.
  • key file.
  • Credentials.

Conclusion

Setting up a publisher-subscriber system has become easier thanks to the MQTT broker. IoT projects and home automation applications benefit greatly from their ease of usage. With the help of this article, you’ll be able to make the most of all of your MQTT-enabled products and understand how they all work together.

The following are some of the new and intriguing features in MQTT 5:

  • User Properties and Custom Headers Voicemails.
  • Content Types and Payload Format Queues for phone calls
  • Options for connecting.
  • Expiry of Message
  • An identifier for a subscription.

Set Up Your Own MQTT Broker on Azure

Stay Connected!

Are you looking for Cloud-based Solutions?

Book your FREE call with our technical consultant now.
Let's Build Your App

Book your FREE call with our technical consultant now.

Totally enjoyed working with Karan and his team on this project. They brought my project to life from just an idea. Already working with them on a second app development project.

They come highly recommended by me.

Martins
OwnerOwner, Digital Babies