# 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 drop-down list Server, select Add a new mqtt-broker....
5. Click the Pen icon to open the Add new mqtt-broker config node dialogue.
6. In the text box Name, enter a name for the node.
7. Select the Connection tab.
8. In the text box Server, enter the adress of the MQTT broker.
9. In the text box Port, enter the port you want to use.

10. ## NOTICE

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

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

15. ## NOTICE

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

16. If you want to use clean sessions everytime the connection is established, select the check box Use clean session.

17. ## NOTICE

    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, select the check box Use legacy MQTT 3.1 support.
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.

22. ## NOTICE

    MQTT offers ways to inform all participants about the network status of a particular client.

    1. Birth message: sent by the client, when the connection is established
    2. Close message: sent by the client, before the connection is closed
    3. Will message: sent by the broker, when the client is disconnected unexpectedly
23. In the text boxes Topic, enter the topic to which the respective message shall be published.
24. In the text boxes Payload, enter the payload of the respective message.
25. In the drop-down lists Retain, select whether the respective message shall be retained on the broker.
26. In the drop-down lists QoS, select the Quality of Service level of the respective message. Choose from:
27. 0 (fire and forget): Arrival of the message solely relies on the underlying Internet Protocol network. Message loss may occur.
28. 1 (at least once): The message is assured to arrive, but may arrive more than once.
29. 2 (once and once only): The message is assured to arrive once and only once.
30. 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.