Search and contents

MQTT System Connector

How a MES or PLC / Generic Device Node reads from a site's own plant MQTT broker, the one the site runs and Kindred does not control. Kindred subscribes to the topic filters you list, and each message becomes one raw record. The record goes through the same Data Transformation Layer, Identity Resolution and Trigger Dispatch Queue path an OPC UA record takes, so a mapping that works for one source key works for the other.

Kindred's own brokers, the ones Nodes use to talk to each other, are a different thing: they authenticate by certificate and are configured by the site CA scripts. This document is only about connecting to a broker you already have.

What it can and cannot do

It can:

  • Subscribe to topic filters (+ and # allowed) on a broker reached by host and port, or by a Unix socket on the same machine.
  • Verify the broker with a CA bundle (TLS 1.2 or later, hostname checked), and authenticate with a username and password, a client certificate, or both.
  • Read a UTF-8 JSON object, whose keys are source keys, or a bare JSON number, string or boolean, which is the source key value.
  • Keep its place at the broker (a MES Node by default; a PLC / Generic Device Node defaults to a clean session unless its section sets persistent_session): a message is acknowledged only once it is ingested, and one published while the Node was down is delivered when it returns (see Delivery).

It cannot:

  • Read Sparkplug B or any binary payload. A payload that is not UTF-8 JSON is dropped and counted.
  • Publish to the broker.
  • Build one record from several topics. One message is one record.
  • Use shared subscriptions ($share/...), or QoS 2 together with a persistent session.
  • Replay what the broker no longer holds. The broker's own retention and queue limits decide how much of a downtime is recovered.

A MES Node's config carries exactly one source, opcua, mqtt or database_log. A config with more than one, or none, is refused when it loads. The deviation_flagging, costing_variance, material_consumption and instruction_bindings sections each read a field of every record beside mqtt and name no OPC UA node; the form that names a node is refused there. What is allowed beside which source has the table.

What the site provides

  • The broker's address and port, and the CA certificate that signed its certificate.
  • The topics the data is published on, and a payload that is a JSON object. Agree the key names with whoever owns the publisher.
  • A credential if the broker requires one: a username and password, or a client certificate.
  • An access rule on the broker that lets this client subscribe to those filters.
  • A client id of its own for each Node (the default, kindred-<node instance id>, is unique per Node as long as instance ids are).
  • Broker settings that keep a session across a disconnect. For Mosquitto that is persistence true, a persistent_client_expiration longer than the downtime you want to ride out, and a max_queued_messages large enough for the messages published in that time. max_inflight_messages limits how many unacknowledged messages the broker sends at once; Kindred holds back the rest by not acknowledging until a message is ingested.

Configuration reference

The section is the mqtt key of the Node's config. Every field below is optional unless marked.

FieldDefaultMeaning
host Broker host name or address. Give this or socket_path (required, one of the two)
socket_path Absolute path of a Unix socket. No port, no TLS, no credentials
port 8883 with ca_bundle_path, otherwise 1883 Broker port
ca_bundle_path CA certificate file to verify the broker with. Setting it turns TLS on
certificate_path, private_key_path Client certificate and key, together; need ca_bundle_path
username With password; both or neither
password Secret. Needs ca_bundle_path: a password is never sent over a connection that is not TLS
client_id kindred-<node instance id> The id the broker knows this client by. Needed for a persistent session
persistent_session MES: on (off with a QoS 2 subscription). PLC / Generic Device: off, since a reading is counted the moment it arrives. The connector itself defaults to off Keep the session at the broker and acknowledge a message only once it is ingested
subscriptions required A list of {topic, qos}. qos is 0, 1 or 2 and defaults to 1
id_field A key in the payload that identifies the message. Recommended; see Delivery
max_payload_bytes 262144 A larger payload is dropped and counted
source_keys The payload keys the mapping uses. Each must be projected by field_map or state_transition_rules, or the Node refuses to start
field_map Canonical field to source key, for the Explicit mapping
state_transition_rules Rules that classify a record into canonical fields, for the Inferred mapping
qualification_relevant true Whether an order release from this source runs First Article Inspection

topic and value are always available as source keys and never need declaring. The message's own topic is topic, and it wins over a payload key of the same name.

A worked example

{
  "host": "broker.plant.example",
  "ca_bundle_path": "/etc/kindred/plant-ca.pem",
  "username": "kindred-reader",
  "password": "set-this-through-the-encrypted-config",
  "subscriptions": [{ "topic": "plant/line1/orders/#" }],
  "id_field": "seq",
  "source_keys": ["job_id", "state"],
  "field_map": { "order_id": "job_id", "status": "state" }
}

A message published on plant/line1/orders/release with this payload:

{ "seq": 4121, "job_id": "MO-2027-0412", "state": "released" }

becomes the record {"seq": 4121, "job_id": "MO-2027-0412", "state": "released", "topic": "plant/line1/orders/release"}, and the Explicit mapping turns it into these canonical fields:

{ "order_id": "MO-2027-0412", "status": "released" }

Supplying the password and confirming TLS

Put the password in the Node's config and keep that config encrypted: a file named *.enc.json is a SOPS file, decrypted in memory when the Node loads it. The password field is marked secret, so it is replaced by a placeholder in GET /v1/config, left out of every validation error, and never appears in a log or in GET /v1/status.

TLS is on whenever ca_bundle_path is set. There is no fallback: if the broker's certificate does not verify, the connection stays down and is not retried in plaintext. Two ways to confirm it: GET /v1/status shows the target as mqtts://host:port, and a config with a password but no ca_bundle_path is refused when it loads.

Delivery

What counts as one message. A message that arrives on a topic matching a subscription is one record. If two filters match the same topic, the message is handled once, under the first filter listed.

Identity of a message. With id_field set, a message is identified by that key's value together with its topic, so two machines publishing the same sequence number stay apart. The values 7, 7.0 and "7" are one id. Without id_field, or when a message lacks the key, it is identified by a hash of its topic and payload, which means two byte-identical messages are one message. That is right for a state change and wrong for a repeating reading, so set id_field whenever the publisher numbers its messages. A message that lacks the configured key is counted (id_field_missing).

At least once, ingested once. The broker may deliver a message more than once (a redelivery, or a retained message again after a reconnect). Every delivery of a message carries the same identity, and the Trigger Dispatch Queue ignores a second Trigger with the same identity, so one message produces one Trigger. Retained messages are ingested when the Node subscribes.

Acknowledged after ingest. This and the recovery of messages published during downtime apply only while persistent_session is on. That is the default for a MES Node, not for a PLC / Generic Device Node.

  • A Node keeps its session at the broker and tells it a message arrived only after the message is ingested. If the process stops or dies first, the broker delivers the message again when the Node returns.
  • Messages published while the Node was down are delivered then too, for QoS 1 subscriptions, up to the broker's own limits. QoS 0 messages are never held.
  • A Node with a QoS 2 subscription keeps a clean session instead, and loses a message that was in flight when it stopped.

Order and speed. Messages are ingested one at a time, in the order they arrived, on a worker thread, so a slow database never delays the connection's keepalives. Up to 1000 messages wait in memory; the broker's in-flight limit normally keeps the number far below that.

When ingesting a message fails.

  • The database cannot be reached, or is shutting down or out of connections: the Node retries with a growing delay (0.5 s up to 10 s) with no limit. Nothing is lost: the messages wait unacknowledged at the broker.
  • The database refuses the message with an error that can pass, such as a deadlock or a statement timeout: the Node tries up to five times in all.
  • Any other error, or the five tries used up: the message is kept in the mqtt_dead_letters table, logged without its payload, and acknowledged, so it does not hold up the ones behind it.
  • The message cannot be kept either (the table is unavailable for a reason other than the database being unreachable): it is left unacknowledged, with an error line saying so. The broker delivers it again only after the connection drops and returns or the Node restarts, and until then it holds a place in the broker's in-flight window. If several pile up the broker stops sending, so fix the table and restart the Node.

A message that is not a record. Each of these is counted, logged without its payload (the first five of each kind, then every hundredth), and acknowledged. The next valid message is ingested as usual.

  • a payload that is not UTF-8, or not valid JSON, or neither an object nor a scalar;
  • a payload over the size limit, or containing NaN or Infinity;
  • a payload containing a NUL (\u0000) or lone surrogate (\ud800) character, which the database cannot store;
  • a message published on a topic matching no subscription.

The broker going away. The Node reconnects on its own and subscribes again each time. A broker that is down when the Node starts does not stop it.

Reading status and failures

GET /v1/status runs a connection check on the mqtt section as a second session: it connects with a client id of its own (so the broker does not disconnect the live connection), subscribes to nothing, and closes. The check is named mqtt, has connector type mqtt, and its target is mqtts://host:port (mqtt:// without TLS, mqtt+unix://path for a socket). It carries no topic and no payload. The messages you will see:

MessageMeaning
connected to host:port The broker accepted the connection and the credential
authentication failed for host:port: ... The broker refused the username and password
could not connect to host:port: ... Nothing answered, or the certificate did not verify; the reason follows

The check says nothing about the topics or the data. To see whether messages are arriving, look at the Trigger Dispatch Queue and the Activity Log. To see what could not be ingested:

SELECT count(*) FROM mqtt_dead_letters;
SELECT dead_lettered_at, topic, reason, attempts, record FROM mqtt_dead_letters ORDER BY dead_lettered_at;

reason is the exception type. Find the cause in the Node's log, which has one error line for each dead letter (with the topic and message id, never the payload). Once the cause is fixed, publish the message again from the source and delete the row. Nothing reads the table back by itself.

Other lines worth knowing in the log: retrying in ... (the database is not answering; messages are waiting, not lost), ingest queue is full (more messages are in flight than the broker should allow; the message named is acknowledged and dropped, so it is lost, not redelivered: lower max_inflight_messages on the broker to the queue size or below), and not delivered as a record (a payload that was dropped, with the reason).

Trying a config without the plant

To give a config something to read before the plant is reachable, publish a captured file of messages to a broker of your own with the payload player. It is test tooling.

Pitfalls

  • Two Nodes with the same client id evict each other. The broker disconnects the older session when a second connects with the same id. Both then reconnect and take it back, forever. Give each Node a distinct id, or leave client_id unset.
  • Byte-identical messages are one message unless id_field is set. A publisher that repeats the same payload on purpose (a heartbeat, a reading that did not change) will have every repeat after the first ignored.
  • Removing a topic filter does not remove it at the broker. A persistent session keeps the subscriptions it had. The broker goes on sending messages for the old filter, and the Node drops them as matching no subscription (counted, and acknowledged). To clear it, use a new client_id, or remove the session on the broker.
  • A broker without persistence forgets the session when it restarts. Messages published while the broker was restarting, and anything it had not yet delivered, are gone. That is the broker's setting, not the Node's.
  • A QoS 2 subscription turns durable delivery off for the whole Node. Use QoS 1 and id_field instead: the effect is the same, because a repeated message is ignored.
  • A declared source key that nothing projects stops the Node at start. source_keys lists the payload keys the mapping uses, and each must appear in field_map or a rule, so a key is never quietly dropped.
  • A payload key named topic is replaced by the real topic. The record always carries the topic the broker delivered.
  • Numbers too large for a float are refused. 1e999 is an invalid payload, not infinity.

Release: v0.11.0