All Articles

Try Mosquitto Broker

Greetings! It has been almost two years since the last post. So many things happened in my life (especially my kid’s birth), and I had no time to write. Because the situation is improving, I plan to write and publish a post once a week or every two weeks. Speaking of today’s topic, I will share some of my experiences using Eclipse Mosquitto as a broker.

What is Mosquitto?

Eclipse Mosquitto is an open-source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1, and 3.1.

Mosquitto provides a server for the communication protocol called MQTT (Message Queueing Telemetry Transport), designed for Internet of Things messaging by using a publish/subscribe model. Then, about MQTT, I will write another post to explain it. Additionally, I will not explain how to install Mosquitto because it is pretty simple. Please google it if you want to have a try.

How to configure Mosquitto?

It is complex to configure the mosquitto because there are many configurations. As described in mosquitto.conf man page, it is possible to run the mosquitto without a configuration file using the default settings. So, for example, by using the following systemd commands, the mosquitto service starts.

> sudo systemctl start mosquitto
> sudo systemctl status mosquitto

● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2022-12-07 18:26:26 JST; 3 days ago
       Docs: man:mosquitto.conf(5)
   Main PID: 11998 (mosquitto)
      Tasks: 1 (limit: 4915)
        CPU: 1min 45.380s
     CGroup: /system.slice/mosquitto.service
             └─11998 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

We can also look up the default port (1883) used by the mosquitto using the netstat command. Because the port (1883) is not open to the remote hosts, you need to either change the port or open the port if remote access is required.

> sudo netstat -ltnup

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0*               LISTEN      11998/mosquitto
tcp6       0      0 ::1:1883                :::*                    LISTEN      11998/mosquitto

Here are my configuration files for local development use. The settings are minimal. The file in the path /etc/mosquitto provides some default settings, like the path of the PID file, how and where to persist the data, etc. Usually, I keep that file the same but create a new file under /etc/mosquitto/conf.d. For development purposes, I changed the port number and turned on allow_anonymous. If you want to expose the server to the internet, please consider the security and enable the username/password authentication and the encryption (certificate-based or pre-shared-key based) described in the mosquitto.conf man page.

# /etc/mosquitto/mosquitto.conf
pid_file /run/mosquitto/

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

# /etc/mosquitto/conf.d/mosquitto.conf
per_listener_settings true

# MQTT, anonymous for dev@local
listener 11883
allow_anonymous true
set_tcp_nodelay true

# Log
log_timestamp true

Using the settings described above, we can subscribe and publish a topic by connecting with the mosquitto as a broker. So my way of doing those is as follows. By the way, blackpi-4g.local is the hostname of my Raspberry Pi where the mosquitto runs.

# subscribe topic 'test' in Machine A
> mosquitto_sub -h blackpi-4g.local -p 11883 -t test

# publish a message to topic 'test' from Machine B
> mosquitto_pub -h blackpi-4g.local -p 11883 -t test -m "Hello"

# Machine A recieve the message
> mosquitto_sub -h blackpi-4g.local -p 11883 -t test