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

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
Copied!

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
}
Copied!
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
}
Copied!
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
}
Copied!
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
}
Copied!
Last modified 4mo ago