MQTT broker
The MQTT Broker module (io.scadex.broker.mqtt) makes the gateway an MQTT broker, so devices can connect to it, and anything that speaks MQTT can read the plant's tag values as topics. This page is for administrators who set up the broker and engineers who bind tags to its topics.
The broker is NATS. The module runs a second nats-server with its MQTT listener on, apart from the one the gateway uses for tag values. They are kept apart on purpose: NATS turns an MQTT topic into a subject, so a client of the tag backbone's server could read or forge tag values by publishing to the backbone's subject. The broker's server has its own store, listens on the loopback address for the module's own use, and lets nobody in without a login. It speaks MQTT 3.1.1, which is all NATS speaks.
The module declares SDK contract 1.5, for IModuleContext.Drivers. Its kind is integration and its capabilities are api and mqtt-broker. It needs the nats-server program, which the appliance image carries; without it the module still runs and the page says so. It has been tested against the NATS server with the MQTTnet client and Mosquitto's tools. No PLC or plant device has been connected to it.
Turning it on
The broker is off until an administrator turns it on. Open Gateway, then MQTT broker, and select Turn on. The page shows the state, the address it listens on, the clients connected and the logins turned away since it started.
Inside the container the broker listens on 0.0.0.0:1883. What the host offers is set where the gateway is started, in the environment of compose.yaml:
| Variable | Default | Meaning |
|---|---|---|
SCADEX_MQTT_BIND | 127.0.0.1 | The host address the port is published on. The default is this machine only; a LAN address serves the plant network and 0.0.0.0 every interface. |
SCADEX_MQTT_PORT | 1883 | The port on the host. |
Publishing the port to a network is a decision the compose file makes, not the dashboard: until it says so, only this machine reaches the broker.
Settings
The Settings view holds how the broker listens.
| Setting | Default | Meaning |
|---|---|---|
| Address | 0.0.0.0 | The address the broker listens on inside the gateway. |
| Port | 1883 | The port it listens on inside. The container maps the host's port to this one, so change SCADEX_MQTT_PORT rather than this. |
| TLS | off | Serve TLS on the port. Needs a certificate; plain MQTT is then not offered on that port. |
| Largest message | 1024 KiB | A bigger message is refused. From 1 KiB to 64 MiB. |
| Most clients | 0 | Zero leaves it to the server (65,536). |
| Storage | 1024 MB | What the broker may keep on disk for retained messages and client sessions. |
Saving restarts the broker's listener, which disconnects its clients. Plain MQTT sends passwords and values readable: use TLS on anything but a network you trust.
Certificate
For TLS, paste or choose a PEM certificate (with its chain after it) and its private key without a password. The key is kept in the module's folder, readable only by the gateway. The page shows the certificate's subject, names and the date it expires; replacing it while TLS is on restarts the listener, which disconnects the clients. A certificate can't be removed while TLS is on.
Logins
Devices and programs connect with a login made under Logins. A name is 1 to 64 letters, digits, dots, dashes, underscores or @, and a password has at least 8 characters. The broker keeps only a bcrypt hash, so a password can be reset but never read back; Make one fills in a 24-character password and shows it, which is the one time it can be copied.
Each login has two topic limits, Publish and Subscribe: any topic, only the topics listed, or none (a login that cannot publish is read-only). Topics are MQTT filters, one per line; + stands for one level and # for everything below, so plant/# is every topic under plant.
- A message the login may not publish is dropped by the server; MQTT 3.1.1 has no way to tell the sender.
- A subscription wider than the login allows is refused in the broker's reply.
- Topics can't have spaces, asterisks,
>or control characters, which the server can't carry. - Changing or deleting a login disconnects what is connected with it, and only that.
A login can be switched off without deleting it. The gateway's own login, scadex-gateway, is made by the module and isn't listed.
Clients
The Clients view lists who is connected: client id, login, address, how long, subscriptions and messages in and out, and the topics each subscribed to. The gateway's own connections (the tag publisher and the MQTT driver's link) are listed too, marked, because they count in the same totals.
Under the list are the logins the broker turned away, newest first, with the address each came from and the count since the broker started. A broker open to the internet is scanned within hours, so this is the first place to look when a device says it cannot connect.
Publishing tag values
Under Publishing, the module can publish tag values as MQTT topics. It reads the same stream the historian reads.
| Setting | Default | Meaning |
|---|---|---|
| Publish tag values | off | Switches publishing on. |
| Topic prefix | scadex | A tag [provider]Folder.Tag becomes prefix/provider/Folder/Tag. Spaces, slashes, wildcards and > in a name become _. |
| Payload | JSON | json: {"value":…,"quality":"good","timestamp":"…Z"}, with a detail when the quality has one. value: the value alone, as text. |
| Retain | on | The broker keeps each topic's last message and hands it to whoever subscribes next. |
| Quality of service | 0 | 0 or 1. |
| Included, Left out | *, [system]* | Patterns on the tag's path, where * stands for anything. A tag is published when it matches one under Included and none under Left out. |
A topic's newest value waits in memory until the broker has it, so a burst of changes to one tag is one message. When the module is far behind the stream, the first time it is switched on or after a stop, it gathers the newest value of each tag and sends those instead of replaying the history. Changing what is published sends every tag's latest value again in the new form; the topics of the old form stay with the broker.
Binding tags to topics
The module gives the MQTT driver a connection called Scadex broker, with the gateway's own login and MQTT 3.1.1 already set, so tags can be bound to the broker's topics like any other broker's. It appears under Connections, Devices, MQTT, kept by the module: it can be switched off and on, and its address, login and settings can't be changed or deleted by hand. If anything changes it, the module puts it back. See MQTT for binding tags.
API
Everything is under /api/v1/modules/io.scadex.broker.mqtt.
| Request | Role | Purpose |
|---|---|---|
GET /status | viewer | State, counts, messages, publishing state and the driver's connection. No names or addresses. |
GET /settings, PUT /settings | engineer, administrator | Enable, address, port, TLS and limits. |
GET /publishing, PUT /publishing | engineer, administrator | Tag values as topics. |
GET /users, POST /users, PUT /users/{name}, DELETE /users/{name} | engineer, administrator | Logins; PUT also resets a password. |
GET /clients | engineer | Connected clients, and the latest refused logins with their count. |
GET /tls, PUT /tls, DELETE /tls | engineer, administrator | The certificate and its key, as PEM. |
Reading logins, clients and settings is for engineers and administrators; changing them is for administrators.