MQTT

Detailed documentation on the MQTT pubsub component

Setup MQTT

You can run a MQTT broker locally using Docker:

  1. docker run -d -p 1883:1883 -p 9001:9001 --name mqtt eclipse-mosquitto:1.6.9

You can then interact with the server using the client port: mqtt://localhost:1883

You can run a MQTT broker in kubernetes using following yaml:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: mqtt-broker
  5. labels:
  6. app-name: mqtt-broker
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. app-name: mqtt-broker
  12. template:
  13. metadata:
  14. labels:
  15. app-name: mqtt-broker
  16. spec:
  17. containers:
  18. - name: mqtt
  19. image: eclipse-mosquitto:1.6.9
  20. imagePullPolicy: IfNotPresent
  21. ports:
  22. - name: default
  23. containerPort: 1883
  24. protocol: TCP
  25. - name: websocket
  26. containerPort: 9001
  27. protocol: TCP
  28. ---
  29. apiVersion: v1
  30. kind: Service
  31. metadata:
  32. name: mqtt-broker
  33. labels:
  34. app-name: mqtt-broker
  35. spec:
  36. type: ClusterIP
  37. selector:
  38. app-name: mqtt-broker
  39. ports:
  40. - port: 1883
  41. targetPort: default
  42. name: default
  43. protocol: TCP
  44. - port: 9001
  45. targetPort: websocket
  46. name: websocket
  47. protocol: TCP

You can then interact with the server using the client port: tcp://mqtt-broker.default.svc.cluster.local:1883

Create a Dapr component

The next step is to create a Dapr component for MQTT.

Create the following yaml file named mqtt.yaml

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: pubsub.mqtt
  8. version: v1
  9. metadata:
  10. - name: url
  11. value: "tcp://[username][:password]@host.domain[:port]"
  12. - name: qos
  13. value: 1
  14. - name: retain
  15. value: "false"
  16. - name: cleanSession
  17. value: "false"

To configure communication using TLS, ensure mosquitto broker is configured to support certificates. Pre-requisite includes certficate authority certificate, ca issued client certificate, client private key. Make following additional changes to mqtt pubsub components for supporting TLS.

  1. ...
  2. spec:
  3. type: pubsub.mqtt
  4. metadata:
  5. - name: url
  6. value: "tcps://host.domain[:port]"
  7. - name: caCert
  8. value: ''
  9. - name: clientCert
  10. value: ''
  11. - name: clientKey
  12. value: ''

Where:

  • url (required) is the address of the MQTT broker.
    • use tcp:// scheme for non-TLS communication.
    • use tcps:// scheme for TLS communication.
  • qos (optional) indicates the Quality of Service Level (QoS) of the message. (Default 0)
  • retain (optional) defines whether the message is saved by the broker as the last known good value for a specified topic. (Default false)
  • cleanSession (optional) will set the “clean session” in the connect message when client connects to an MQTT broker . (Default true)
  • caCert (required for using TLS) is the certificate authority certificate.
  • clientCert (required for using TLS) is the client certificate.
  • clientKey (required for using TLS) is the client key.

Warning

The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described here.

Apply the configuration

Visit this guide for instructions on configuring pub/sub components.

Related links

Last modified February 16, 2021: Merge pull request #1235 from dapr/update-v0.11 (b4e9fbb)