This guide provides essential information to quickly install, configure, and use the Express MQTT broker. You can run EMQTT directly, specifying a configuration file:
~$ emqtt -c /path/to/your/emqtt.conf -d
Use -d to run it in the background. If no configuration file is specified, it uses default settings (allowing anonymous connections on port 1883).
Open Terminal 1 (Subscriber):
Subscribe to a topic. The -v flag (verbose) will print the topic along with the message.
~$ emqtt_sub -h localhost -t “home/office/light” -v
Open Terminal 2 (Publisher):
Publish a message to the same topic.
~$ emqtt_pub -h localhost -t “home/office/light” -m “ON”
You should see home/office/light ON appear in Terminal 1. This confirms your broker is working!
Broker: The EMQTT server itself, responsible for receiving messages from publishers and routing them to subscribers.
Client: Any device or application that connects to the broker (e.g., a sensor publishing data, an app subscribing to updates).
Topic: A UTF-8 string that acts as a channel for messages (e.g., sensors/temperature/room1). Topics are hierarchical, using / as a delimiter.
Publish: Clients send messages to specific topics on the broker.
Subscribe: Clients tell the broker they are interested in receiving messages from specific topics (or topic patterns using wildcards).
+: Single-level wildcard (e.g., sensors/+/room1 matches sensors/temperature/room1 but not sensors/temperature/humidity/room1).
#: Multi-level wildcard, must be the last character (e.g., sensors/temperature/# matches sensors/temperature/room1 and sensors/temperature/room1/alerts).
Quality of Service (QoS): Defines the guarantee of message delivery:
QoS 0 (At most once): Fire and forget. Lowest overhead.
QoS 1 (At least once): Guarantees message delivery, but duplicates may occur.
QoS 2 (Exactly once): Guarantees message delivery exactly once. Highest overhead. EMQTT supports all three levels.
EMQTT’s behavior is controlled by its configuration file, typically stored at /etc/emqtt/emqtt.conf on Linux, or specified with the -c flag. If the file is empty or not found, defaults are used.
Key settings:
Listeners: Define network ports and protocols.
# Default MQTT listener on port 1883 for all interfaces
listener 1883
# Listener for MQTT over WebSockets on port 9001
# listener 9001
# protocol websockets
Persistence: Save in-flight messages and client subscriptions across broker restarts.
persistence true
persistence_location /var/lib/emqtt/
# Ensure this directory is writable by the user EMQTT runs as.
Allowing Anonymous Access:
# allow_anonymous true # (Default if no other authentication is set up)
# allow_anonymous false # Disable if using password or other authentication
Logging:
# Log to a file
log_dest file /var/log/emqtt/emqtt.log
# Log types (e.g., error, warning, notice, information, all)
log_type error
log_type warning
# For more detailed logging:
# log_type all
# log_type_websockets true # If using websockets
Remember to restart EMQTT after changing the configuration.
Never run an internet-facing broker without security.
TLS/SSL Encryption: Encrypts all traffic between clients and the broker.
Requires SSL certificates (CA certificate, server certificate, server key).
Configuration example:
Code snippet
listener 8883
cafile /path/to/your/ca.crt
certfile /path/to/your/server.crt
keyfile /path/to/your/server.key
Client Authentication:
Username/Password:
Disable anonymous access: allow_anonymous false
Specify a password file: password_file /etc/emqtt/pwfile
Create and manage the password file using the emqtt_passwd utility:
# Create a new password file (first user)
~$ sudo emqtt_passwd -c /etc/emqtt/pwfile username
# Add/update other users
~$ sudo emqtt_passwd /etc/emqtt/pwfile anotheruser
Ensure the pwfile is readable by EMQTT.
Access Control Lists (ACLs):
Define which authenticated users (or anonymous users, if allowed) can publish or subscribe to specific topics.
Specify an ACL file: acl_file /etc/emqtt/aclfile
ACL file format example:
# User ‘sensor_uploader’ can only write to ‘data/sensors/#’
user sensor_uploader
topic write data/sensors/#
# User ‘dashboard_user’ can only read from ‘data/#’
user dashboard_user
topic read data/#
# Allow any authenticated user to read their own state topic
pattern read %u/state/#
These tools are invaluable for testing, debugging, and scripting.
emqtt_pub (Publisher):
Publishes a single message to a topic.
~$ emqtt_pub -h <host> -p <port> -t <topic> -m <message> [options]
Common options:
-q <0|1|2>: Quality of Service.
-u <username> -P <password>: Authentication.
-l: Read message from stdin, sending each line as a new message.
–cafile <path>: For TLS.
Example: emqtt_pub -h broker.example.com -t “updates” -m “System rebooting” -u admin -P “secret”
emqtt_sub (Subscriber):
Subscribes to topics and prints received messages.
~$ emqtt_sub -h <host> -p <port> -t <topic> [options]
Common options:
-v: Verbose – print topic with the message.
-q <0|1|2>: QoS level for subscription.
-u <username> -P <password>: Authentication.
-C <count>: Exit after receiving count messages.
–cafile <path>: For TLS.
Example:
~$ emqtt_sub -h broker.example.com -t “sensors/#” -v -u user1
emqtt_passwd: (Covered in a previous section.) Creates and manages password files for EMQTT.
Bridging: Configure EMQTT to connect to other MQTT brokers, allowing messages to be shared between them. Useful for federated systems or migrating messages.
MQTT v5 Features: If using MQTTv5 clients, EMQTT supports advanced features like message properties, shared subscriptions (for load balancing subscribers), message expiry, topic aliases, and more.
WebSockets: Allows MQTT communication directly from web browsers by configuring a listener with the websockets protocol.
Brokennib Works uses innovative design and advanced technology to create human centered digital solutions that revolutionize enterprises.
Copyrights © 2025 Brokennib | All Rights Reserved