Many home automation and other internet of things platforms use MQTT as the messaging protocol for communicating between devices. If you already have a broker running somewhere, skip down to these instructions on how to use MQTT with node-red

Installing the MQTT Broker

We will use the mosquitto MQTT broker. It is currently not in the default repositories, so we have to added it first before using apt-get to install it. First, add the keys:

sudo wget
sudo apt-key add mosquitto-repo.gpg.key
sudo rm mosquitto-repo.gpg.key

And then add the repos to the apt sources list:

cd /etc/apt/sources.list.d/
sudo wget

(or mosquitto-wheezy.list if running older version of Raspbian. If not sure which one you have, run lsb_release -a)
Finally, update and install the broker application and clients:

sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

You can launch the broker in daemon mode using mosquitto -d 

MQTT Client Example using Node-red


In node-red the “mqtt” node is a built in output node. Following shows the addition of an output node to the basic node-red sensor example:


Next, we can configure the broker (Server address) and the message topic of the node:


use localhost for the server if the broker is running on the Pi itself. The topic is where you can format the address. When you deploy and start the sensor polling, you should be able to receive messages subscribed to the same topic. Below shows a the mosquitto command-line client mosquitto_sub receiving the messages published to the topic pishield/sensor1:



Of course, you can use the node-red mqtt input node to subscribe to topics as well to receive messages!

Further Reading

  • If you want to make the MQTT run on startup and in persistence mode, check out this guide here. Persistence mode allows the broker to restore any subscriptions when a client reconnects, which can make things a bit cleaner (your client doesn’t have to resubscribe to topics each time).