Skip to main content

TLS in Node-RED for MQTT

Use TLS in Node-RED for MQTT

Abstract

Use TLS in Node-RED for MQTT

This tutorial uses Node-RED to establish a secure MQTT connection between FairCom servers and client applications.

Prerequisites
  • Ensure you have server and client certificate files created, configured, and copied to the appropriate computers.

    You must create a separate server certificate for each computer that runs a FairCom server. Each FairCom server must be configured to use the appropriate server certificate for the computer on which it runs

  • Each user must have its own client certificate. A client certificate allows a client program to identify itself to a FairCom server. Each program that wants to use a certificate must be configured to send the certificate to the server. Do not use a client certificate in a client program if the FairCom server is not configured with a CA certificate.

  1. Create a new flow.

  2. Add these four nodes: "mqtt in", "debug", "inject", and "mqtt out".

  3. Wire the output of the "mqtt in" node to the input of the "debug" node.

  4. Wire the output of the "inject" node to the input of the "mqtt out" node.

  5. Double-click the "inject" node and change the msg.payload drop-down to "timestamp".

  6. Change its name to "Timestamp".

  7. Click "Done".

    The flow should look like this:

    Node-RED_wired_up_nodes.svg
  1. Double-click on the "mqtt out" node to configure the node and create a connection to the broker.

  2. On the "Topic" field, enter test/Node-RED/certificate_timestamp.

  3. Set "QoS" to 1.

  4. Set "Retain" to "false".

    Leave the name blank.

    The node should look like this:

    Node-RED_mqtt_out_node.svg
  1. On the "Server" row, click the drop-down menu and select "Add new mqtt-broker…".

  2. Click the pencil icon to the right of that drop-down menu to create the server connection.

  3. Give the connection a name such as "FairCom MQTT with certificates".

  4. Set the "Server" field to the network address of the FairCom MQTT broker.

  5. Set the "Port" to the MQTTS port of the FairCom MQTT broker (typically 8883).

  6. Leave "Connect automatically" checked.

  7. Check the "Use TLS" box.

    The connection should look like this:

    Node-RED_broker_connection.svg
  1. Click the pencil icon to the right of the "Use TLS" drop-down menu to create the new TLS configuration.

    If "Use key and certificates from local files" is checked, the files will be loaded from the file system every time Node-RED is started or redeployed.  If it is unchecked, the certificates will be uploaded to Node-RED and the files can be removed from the file system.  In this tutorial, we leave the box unchecked.

  2. Click the "Upload" button next to "Certificate" to select the client certificate to use.

    If your certificate contains both the client certificate and client key, you do not need to upload a key file.

    If your client key is in its own file, click the "Upload" button next to "Private Key" to select the client key to use.

  3. Click the "Upload" button next to "CA Certificate" to select the Certificate Authority certificate to use.

  4. Leave "Verify server certificate" checked.

    The TLS configuration should look like this:

    Node-RED_certificate_configuration.svg
  5. Click "Update".

  6. On the "Edit mqtt-broker node" screen, ensure the TLS drop-down menu shows the TLS configuration you just created, and click "Update".

  7. On the "Edit mqtt out node" screen, ensure the Server drop-down menu shows the broker connection you just created, and click "Done".

  8. Double-click the "mqtt in" node.

  9. For the server, select the drop-down menu and select the server you just configured.

  10. Leave the "Action" set to "Subscribe to single topic".

  11. Set the "Topic" to test/Node-RED/certificate_timestamp.

  12. Set the "QoS" to 1.

  13. Leave the "Output" set to "auto-detect".

  14. Leave the "Name" blank.

    The "mqtt in node" should look like this:

    Node-RED_mqtt_in_node.svg
  15. Optionally rename the debug node.

  16. Click "Deploy".

  17. Click the blue square to the left of the inject node.

    A timestamp is sent to the "mqtt out" node.

    The flow should look like this:

    Node-RED_final_flow.svg