Install MQTT Broker (Mac)
Mosquitto is a popular open-source MQTT broker. Here’s a step-by-step guide to installing and running Mosquitto on a system running MacOS. This tutorial assumes that you use Brew as your package manager. Other package managers probably work, but the commands need to be changed accordingly to the used package manager.
Step 1: Install Mosquitto
-
Update the package list:
brew upgrade -
Install Mosquitto and its clients:
brew install mosquitto
Step 2: Set Up User and Password for MQTT Broker
-
Create a password file for Mosquitto. This is where you choose your username. Make sure the path to the password file path is the same as set in the previous step.
mosquitto_passwd -c /opt/homebrew/etc/mosquitto/passwd yourusername -
Enter the password when prompted. After that you will have to confirm it by entering it a second time.
This project expects you to use knightec as your username and password, read more here.
Step 3: Configure Mosquitto
Any files with extension .conf that are placed in /opt/homebrew/etc/mosquitto/ will be used by the MQTT Broker.
-
Add or open the configuration file in a text editor:
nano /opt/homebrew/etc/mosquitto/mosquitto.conf -
Add these lines to configure the MQTT Broker to listen to port 1883 and require authentication.
# If this is set to true then authentication is disabled.
allow_anonymous false
# Default is /opt/homebrew/etc/mosquitto/passwd, but any path could be used.
password_file /opt/homebrew/etc/mosquitto/passwd
# Another port can be used, but 1883 is default.
listener 1883
protocol mqtt
listener 8080
protocol websocketsPress Ctrl + X to exit. It will ask you to save your changes.
Press Y to save.
It will ask you which file name to save to. It's already set to the current file, so just press Enter.
Summary: The sequence of keystrokes to change your changes in nano text editor is Ctrl + X -> Y -> Enter.
Step 4: Start the Mosquitto Service
These steps allow you to add Mosquitto as a user service. It is also possible to add it as a system-wide/root service, but it should not be necessary.
-
Link the .plist to be able to use it as a service
sudo ln -sfv /opt/homebrew/opt/mosquitto/homebrew.mxcl.mosquitto.plist /Library/LaunchAgents -
Enable the Mosquitto service:
launchctl enable user/501/homebrew.mxcl.mosquitto -
Start Mosquitto to and allow it to start on boot:
launchctl bootstrap user/501 /opt/homebrew/opt/mosquitto/homebrew.mxcl.mosquitto.plist -
Check the status of the Mosquitto service:
launchctl print user/501/homebrew.mxcl.mosquitto
Step 5: Test the Mosquitto Broker
-
Open a terminal and subscribe to a topic (e.g.,
test/topic):mosquitto_sub -h localhost -t test/topic -u "yourusername" -P "yourpassword" -
Open another terminal and publish a message to the same topic:
mosquitto_pub -h localhost -t test/topic -m "Hello, MQTT" -u "yourusername" -P "yourpassword"
You should see the message "Hello, MQTT" appear in the terminal where you subscribed to the topic.
Additional Tips
-
Logging: To be able to more easily solve issues, logging can be enabled for the service. The 'syslog' option does not work for newer versions of MacOS. Instead, to enable logging to a specific logfile add the following to the configuration file (
/opt/homebrew/etc/mosquitto/mosquitto.conf).log_dest file /Users/YourUser/Library/Logs/Homebrew/mosquitto/mosquitto.logReplace 'YourUser' with the username of your local account. You can then look at the logfile using any file editor of your choice, or by running
tail -f /Users/YourUsername/Library/Logs/Homebrew/mosquitto/mosquitto.logto continuously output new content in the logfile to a chosen a terminal window. -
Restarting a service: To restart the Mosquitto service after being enabled and started, use the following command:
launchctl kickstart -k user/501/homebrew.mxcl.mosquitto
Congratulations! You have now installed, configured, and ran an MQTT broker using Mosquitto on your Mac.