# Adding an MQTT broker

You need to add an MQTT broker to subscribe or publish to MQTT topics.

1. Open Node-RED.
2. Add an mqtt node from the input category or the output category to the workspace.
3. Double-click the mqtt node to open the Edit node dialogue.
4. In the Server text box, select Add a new mqtt-broker....
5. Click the Pen icon to open the Add new mqtt-broker config node dialogue.
6. In the Name text box, enter a name for the node.
7. Select the Connection tab.
8. In the Server text box, enter the adress of the MQTT broker.
9. In the Port text box, enter the port you want to use.

10. We recommend enabling transport layer security (TLS), especially if you are using a username and a password to authenticate to an MQTT broker.

11. Enable the Enable secure (SSL/TLS) connection check box.
12. In the TLS Configuration drop-down list, select a TLS configuration to use or add a new TLS configuration.
13. In the Client ID text box, enter a client ID unique to the broker or leave it blank for autogenerating a client ID.
14. In the Keep alive time (s) text box, enter the time you want the broker to keep the TCP connection open without receiving a package.

15. Setting the keep alive time to zero, disables the keep alive function.

16. If you want to use clean sessions every time the connection is established, enable the check box Use clean session.

17. Use clean sessions if you are using the MQTT broker only for publishing or if it does not matter, that the client misses messages while being offline.

18. If you want to enable MQTT 3.1 support, enable the Use legacy MQTT 3.1 support check box.
19. Select the Security tab.
20. In the corresponding text boxes, enter the username and password for authenticating to the MQTT broker.
21. Select the Messages tab. MQTT offers ways to inform all participants about the network status of a particular client. Choose from:
22. Birth message: sent by the client, when the connection is established
23. Close message: sent by the client, before the connection is closed
24. Will message: sent by the broker, when the client is disconnected unexpectedly
25. In the Topic text boxes, enter the topic to which the respective message shall be published.
26. In the Payload text boxes, enter the payload of the respective message.
27. In the Retain drop-down lists, select whether the respective message shall be retained on the broker.
29. In the QoS drop-down lists, select the Quality of Service level of the respective message. Choose from:
30. 0 (fire and forget): Arrival of the message solely relies on the underlying Internet Protocol network. Message loss may occur.
31. 1 (at least once): The message is assured to arrive, but may arrive more than once.
32. Click Done to apply your changes.

1. An MQTT config node is added. This node establishes a single connection to the broker, which can be used by MQTT nodes to publish or subscribe to topics on that broker.

Publishing messages to an MQTT topic

Subscribing to an MQTT topic