MQTT EMQX

Mosquitto mqtt broker is not suitable for large-scale applications, while EMQX is designed for high concurrency and massive connections, and supports clustering. Let’s see how to install and use EMQX on a Linux server.

Installation

1. download and install EMQX

my server is x86_64 architecture and ubuntu 20.04, so I choose the corresponding version on this page: https://www.emqx.com/en/downloads-and-install/enterprise?os=Ubuntu
and install it following the instructions

2. start EMQX
sudo systemctl start emqx
sudo systemctl enable emqx
sudo systemctl status emqx
sudo emqx ctl status
3. test EMQX

install mqtt client

sudo apt install -y mosquitto-clients

subscribe a topic in a terminal

mosquitto_sub -h 127.0.0.1 -t test/topic

publish a message in another terminal

mosquitto_pub -h 127.0.0.1 -t test/topic -m "hello emqx"
4. config EMQX

the default configuration file is located at /etc/emqx/emqx.conf, you can edit it according to your needs.

in a production environment, you should config EMQX to secure it, for example, set username and password for clients to connect EMQX.
with EMQX 5.10.1, you can set username and password on the dashboard, the default address is http://127.0.0.1:18083.
and the default username is admin, password is public. you can change the password after login.
then on the Dashboard -> Access Control -> Authentication, click Create and select Password-Based and Built-in Database, and save.
then click and enter into the Built-in Database, and Add a user on the User Management tab.

5. test with username and password

subscribe a topic in a terminal

mosquitto_sub -h 127.0.0.1 -t test/topic -u your_username -P your_password

publish a message in another terminal

mosquitto_pub -h 127.0.0.1 -t test/topic -m "hello emqx" -u your_username -P your_password

clients

use python paho-mqtt client to publish in a topic
and use react mqtt client to subscribe the topic and display the message

EMQX default supports websocket, you can use this command to check it:

$ emqx ctl listeners
tcp:default
  listen_on       : 0.0.0.0:1883
  acceptors       : 16
  proxy_protocol  : false
  enbale          : true
  running         : true
  current_conn    : 0
  max_conns       : 1048576
  shutdown_count  : [{bad_username_or_password,4},{tcp_closed,1}]
ssl:default
  listen_on       : 0.0.0.0:8883
  acceptors       : 16
  proxy_protocol  : false
  enbale          : true
  running         : true
  current_conn    : 0
  max_conns       : 1048576
  shutdown_count  : [{bad_username_or_password,1},{malformed_packet,1}]
ws:default
  listen_on       : 0.0.0.0:8083
  acceptors       : 16
  proxy_protocol  : false
  enbale          : true
  running         : true
  current_conn    : 1
  max_conns       : infinity
  shutdown_count  : []
wss:default
  listen_on       : 0.0.0.0:8084
  acceptors       : 16
  proxy_protocol  : false
  enbale          : true
  running         : true
  current_conn    : 0
  max_conns       : infinity
  shutdown_count  : []

brower can’t use tcp protocol, so we need to use websocket protocol to connect EMQX.
so the python paho-mqtt client should be like this:

import paho.mqtt.client as mqtt
mqttc = mqtt.Client(
    mqtt.CallbackAPIVersion.VERSION2, transport="websockets"
)
mqttc.username_pw_set(username, password)
# port should be 8083 for websocket
mqttc.connect(host, 8083)
mqttc.loop_start()

and the react mqtt client should be like this:

import mqtt from "mqtt";

// port should be 8083 for websocket
const client = mqtt.connect("ws://localhost:8083", {
  username: process.env.REACT_APP_MQTT_USERNAME,
  password: process.env.REACT_APP_MQTT_PASSWORD,
  clientId: `web_${Math.random().toString(16).slice(3)}`,
  keepalive: 60,
  clean: true,
  reconnectPeriod: 1000,
});

wss (websocket secure) is also supported by EMQX, but you need to config SSL certificate for it.