Deploying Message Brokers

Setting-up the EMQX Broker

The EMQX broker is a fully open source, highly scalable, highly available distributed MQTT messaging broker for IoT, M2M and Mobile applications  that can handle tens of millions of concurrent clients.

More details can be found on EMQX Website.

Package Installation (Linux)

Download the EMQX DEB package on GitHub

Install EMQX Broker:

sudo apt install ./emqx-ubuntu18.04-v4.0.0_amd64.deb
sudo apt install ./emqx-debian10-v4.0.0_amd64.deb

Start/Stop EMQX Broker

Quick start:

emqx start
emqx_ctl status

Stop EMQX Broker:

emqx stop

This guide can be found on: EMQZ install quide

Use the following Cluster guide to create a cluster of EMQX brokers.

Running EMQX in Docker

Run a single container

Get docker image from Docker Hub

docker pull emqx/emqx:v4.0.0

Start docker container

docker run -d --name emqx -p 1883:1883 -p 8081:8081 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqx/emqx:v4.0.0

Use the guide on: EMQX docker cluster quide to setup a cluster on Docker.

Setting-up the EMQX Dashboard

EMQX Broker provides Dashboard to facilitate users to manage equipment and monitor related indicators. Through Dashboard, you can view the basic information of the server, load and statistical data, you can view the connection status of a client and even disconnect it, and you can also dynamically load and unload specified plug-ins. In addition, EMQX Dashboard also provides a visual operation interface of the rule engine, and also integrates a simple MQTT client tool for user testing.

Use the guide on: EMQX Dashboard to deploy the EMQX dashboard.

Setting-up the Mosquitto MQTT-based Broker

Mosquitto is an open-source message broker that uses the Message Queuing Telemetry Transport (MQTT) Protocol.

More details can be found on Mosquitto Website.

Prerequisites

  • An Ubuntu 20.04 server
  • A non-root user with sudo rights

Install the Mosquitto broker

Update the package information index:

sudo apt update 

Install the mosquitto package

sudo apt install -y mosquitto

The mosquitto package should now load on your server. Confirm the status of the mosquitto service.

sudo systemctl status mosquitto

Ensure the package is loaded and active. Once running, you can manage the mosquitto services by executing the following commands.

Stop/start/restart the mosquitto service:

sudo systemctl stop/start/restart mosquitto

Install and Test the Mosquitto Clients

When using an MQTT client, you can connect to the Mosquitto broker to send/receive messages using different topics. A client can either be a publisher, a subscriber, or both.

The Mosquitto package ships with a command-line client that allows you to test the server functionalities. Install the client.

sudo apt install -y mosquitto-clients

To subscribe to a topic, execute the mosquitto_sub -t command followed by the name of the topic that you want to subscribe to. For example, to subscribe to the home/lights/sitting_room topic, execute.

mosquitto_sub -t "home/lights/sitting_room"

Note that the above command has a blocking function – your shell terminal will be in listening state.

Open a second terminal window to publish the “ON” message to the topic home/lights/sitting_room using the mosquitto_pub -m command.

mosquitto_pub -m "ON" -t "home/lights/sitting_room"

Next, publish an OFF message still on the same home/lights/sitting_room topic on your second terminal

mosquitto_pub -m "OFF" -t "home/lights/sitting_room"

Secure the Mosquitto Server

By default, the Mosquitto broker is not secured. However, you can make some configuration settings to secure it with a username and password.

Mosquitto reads the configuration information from:

/etc/mosquitto/conf.d

Create a default.conf under the directory

sudo nano /etc/mosquitto/conf.d/default.conf

Use the information below to disable anonymous connections and allow Mosquitto to read valid credentials from the /etc/mosquitto/passwd file.

allow_anonymous false
password_file /etc/mosquitto/passwd

Save and close the file. Open the /etc/mosquitto/passwd file using nano

sudo nano /etc/mosquitto/passwd

Then, populate the file with the account details for the users that you want to connect to the Mosquitto server. Replace EXAMPLE_PASSWORD and EXAMPLE_PASSWORD_2 with strong values.

john:EXAMPLE_PASSWORD
george:EXAMPLE_PASSWORD_2

Save and close the file. Next, use the mosquitto_passwd utility to encrypt the passwords.

sudo mosquitto_passwd -U /etc/mosquitto/passwd

Your passwords are now encrypted in a format that only the Mosquitto server can decrypt. Use the Linux cat command to confirm the encryption process.

sudo cat /etc/mosquitto/passwd

Restart the mosquitto service to load the new changes.

sudo systemctl restart mosquitto

From this point forward, you should execute any pub/sub command using the syntax below. Remember to replace john and EXAMPLE_PASSWORD with the credentials that you defined in the password file.

mosquitto_sub -u john -P EXAMPLE_PASSWORD -t "home/lights/sitting_room"
mosquitto_pub -u john -P EXAMPLE_PASSWORD -t "home/lights/sitting_room" -m "ON"
Previous
Next