description: MQTT Configuration Example

Configuration

To enable MQTT support in the server, add an mqtt configuration block in the server’s configuration file like the following:

  1. mqtt {
  2. # Specify a host and port to listen for websocket connections
  3. #
  4. # listen: "host:port"
  5. # It can also be configured with individual parameters,
  6. # namely host and port.
  7. #
  8. # host: "hostname"
  9. port: 1883
  10. # TLS configuration.
  11. #
  12. tls {
  13. cert_file: "/path/to/cert.pem"
  14. key_file: "/path/to/key.pem"
  15. # Root CA file
  16. #
  17. # ca_file: "/path/to/ca.pem"
  18. # If true, require and verify client certificates.
  19. #
  20. # verify: true
  21. # TLS handshake timeout in fractional seconds.
  22. #
  23. # timeout: 2.0
  24. # If true, require and verify client certificates and map certificate
  25. # values for authentication purposes.
  26. #
  27. # verify_and_map: true
  28. }
  29. # If no user name is provided when an MQTT client connects, will default
  30. # this user name in the authentication phase. If specified, this will
  31. # override, for MQTT clients, any `no_auth_user` value defined in the
  32. # main configuration file.
  33. # Note that this is not compatible with running the server in operator mode.
  34. #
  35. # no_auth_user: "my_username_for_apps_not_providing_credentials"
  36. # See below to know what is the normal way of limiting MQTT clients
  37. # to specific users.
  38. # If there are no users specified in the configuration, this simple authorization
  39. # block allows you to override the values that would be configured in the
  40. # equivalent block in the main section.
  41. #
  42. # authorization {
  43. # # If this is specified, the client has to provide the same username
  44. # # and password to be able to connect.
  45. # # username: "my_user_name"
  46. # # password: "my_password"
  47. #
  48. # # If this is specified, the password field in the CONNECT packet has to
  49. # # match this token.
  50. # # token: "my_token"
  51. #
  52. # # This overrides the main's authorization timeout. For consistency
  53. # # with the main's authorization configuration block, this is expressed
  54. # # as a number of seconds.
  55. # # timeout: 2.0
  56. #}
  57. # This is the amount of time after which a QoS 1 message sent to
  58. # a client is redelivered as a DUPLICATE if the server has not
  59. # received the PUBACK packet on the original Packet Identifier.
  60. # The value has to be positive.
  61. # Zero will cause the server to use the default value (30 seconds).
  62. # Note that changes to this option is applied only to new MQTT subscriptions.
  63. #
  64. # Expressed as a time duration, with "s", "m", "h" indicating seconds,
  65. # minutes and hours respectively. For instance "10s" for 10 seconds,
  66. # "1m" for 1 minute, etc...
  67. #
  68. # ack_wait: "1m"
  69. # This is the amount of QoS 1 messages the server can send to
  70. # a subscription without receiving any PUBACK for those messages.
  71. # The valid range is [0..65535].
  72. #
  73. # The total of subscriptions' max_ack_pending on a given session cannot
  74. # exceed 65535. Attempting to create a subscription that would bring
  75. # the total above the limit would result in the server returning 0x80
  76. # in the SUBACK for this subscription.
  77. # Due to how the NATS Server handles the MQTT "#" wildcard, each
  78. # subscription ending with "#" will use 2 times the max_ack_pending value.
  79. # Note that changes to this option is applied only to new subscriptions.
  80. #
  81. # max_ack_pending: 100
  82. }

Server name

MQTT requires a server name to be set. Server names must be unique in a cluster or super-cluster topology. The server name is set in the top-level section of the server configuration. Here is an example:

  1. server_name: "my_mqtt_server"
  2. mqtt {
  3. port: 1883
  4. ...
  5. }

Authentication/Authorization of MQTT Users

Operator mode

In operator mode, all users need to provide a JWT in order to connect. In the standard authentication procedure of this mode, NATS clients are required to sign a nonce sent by the server using their private key (see JWTs and Privacy). MQTT clients cannot do that, therefore, the JWT is used for authentication, removing the need of the seed. It means that you need to pass the JWT token as the MQTT password and use any username (except empty, since MQTT protocol requires a username to be set if a password is set). The JWT has to have the Bearer boolean set to true, which can be done with nsc:

  1. nsc edit user --name U --account A --bearer

Local mode

A new field when configuring users allows you to restrict which type of connections are allowed for a specific user.

Consider this configuration:

  1. authorization {
  2. users [
  3. {user: foo password: foopwd, permission: {...}}
  4. {user: bar password: barpwd, permission: {...}}
  5. ]
  6. }

If an MQTT client were to connect and use the username foo and password foopwd, it would be accepted. Now suppose that you would want an MQTT client to only be accepted if it connected using the username bar and password barpwd, then you would use the option allowed_connection_types to restrict which type of connections can bind to this user.

  1. authorization {
  2. users [
  3. {user: foo password: foopwd, permission: {...}}
  4. {user: bar password: barpwd, permission: {...}, allowed_connection_types: ["MQTT"]}
  5. ]
  6. }

The option allowed_connection_types (also can be named connection_types or clients) as you can see is a list, and you can allow several types of clients. Suppose you want the user bar to accept both standard NATS clients and MQTT clients, you would configure the user like this:

  1. authorization {
  2. users [
  3. {user: foo password: foopwd, permission: {...}}
  4. {user: bar password: barpwd, permission: {...}, allowed_connection_types: ["STANDARD", "MQTT"]}
  5. ]
  6. }

The absence of allowed_connection_types means that all types of connections are allowed (the default behavior).

The possible values are currently:

  • STANDARD
  • WEBSOCKET
  • LEAFNODE
  • MQTT

Special permissions

When an MQTT client creates a QoS 1 subscription, this translates to the creation of a JetStream durable subscription. To receive messages for this durable, the NATS Server creates a subscription with a subject such as $MQTT.sub. and sets it as the JetStream durable’s delivery subject.

Therefore, if you have set some permissions for the MQTT user, you need to allow subscribe permissions on $MQTT.sub.>.

Here is an example of a basic configuration that sets some permissions to a user named “mqtt”. As you can see, the subscribe permission $MQTT.sub.> is added to allow this client to create QoS 1 subscriptions.

  1. listen: 127.0.0.1:4222
  2. jetstream: enabled
  3. authorization {
  4. mqtt_perms = {
  5. publish = ["baz"]
  6. subscribe = ["foo", "bar", "$MQTT.sub.>"]
  7. }
  8. users = [
  9. {user: mqtt, password: pass, permissions: $mqtt_perms, allowed_connection_types: ["MQTT"]}
  10. ]
  11. }
  12. mqtt {
  13. listen: 127.0.0.1:1883
  14. }