Quick Start: Experience Namespaces
This section guides you through using the MQTTX client to connect to EMQX and quickly experience the core capabilities of the namespace feature: tenant identification, client isolation, and topic isolation.
Enable the tns
Attribute for Namespace Identification
First, configure a client attribute in
base.hocon
to extract the namespace (tenant identifier) from the username:mqtt.client_attrs_init = [{expression = "nth(1, tokens(username, '-'))", set_as_attr = tns}]
Example: If a client connects with the username
tenantA-user1
, EMQX will extracttenantA
as the namespace (tns
).Alternatively, you can configure this in the Dashboard:
Create an MQTT client connection using MQTTX, simulating tenant
tenantA
, and set the username totenantA-user1
. Connect the client to EMQX.Go to the Namespace page in the Dashboard and disable the View Explicitly Created Namespace Only toggle. You should see the automatically created namespace
tenantA
.Click Clients in the Actions column to view the client connected to this namespace.
Configure and Verify Namespace Isolation
To isolate client IDs and topics between namespaces, add the following configuration to
base.hocon
:mqtt.clientid_override = "concat([client_attrs.tns, '-', clientid])" listener.tcp.default.mountpoint = "${client_attrs.tns}/"
This configuration will:
- Automatically prepend the tenant prefix to the client ID to avoid conflicts.
- Automatically prepend the namespace prefix to topic names for topic-level isolation between tenants.
You can also set this up in the Dashboard:
Use MQTTX to create two MQTT client connections to simulate two tenants:
tenantA
andtenantB
.Client A (Tenant: tenantA):
Parameter Value Client ID client1
Username tenantA-user1
Subscribe test/topic
Client B (Tenant: tenantB):
Parameter Value Client ID client1
Username tenantB-user2
Publish test/topic
Use Client B to publish a message. Verify the result in MQTTX and the EMQX Dashboard:
- Although both clients use the same client ID (
client1
), due to the prefix rule, they connect astenantA-client1
andtenantB-client1
, avoiding conflicts. - Even though both clients use the same topic (
test/topic
), Client A will not receive messages published by Client B because they are isolated by namespace. - In the Monitoring -> Clients page:
- Client A's subscribed topic appears as
tenantA/test/topic
. - Client B's published topic appears as
tenantB/test/topic
.
- Client A's subscribed topic appears as
- Although both clients use the same client ID (