Skip to Content
On this page

MQTT collection server

The MQTT Collection Server listens for MQTT clients and maps fields from valid JSON messages to gateway variables. RawString controls write publishing; it does not enable telemetry mapping from arbitrary non-JSON text.

Compatible devices and models

Device series / model examplesConnection requirements
Devices or gateways reporting as MQTT clientsDevices connect to the gateway MQTT service with matching authentication, topics and payload mapping. Check the device MQTT version.

Start the listener and connect a publisher

The MQTT collection server provides an MQTT broker listener on the gateway. Equipment or tools such as MQTTX connect to it and publish JSON. The gateway maps each message topic and JSONPath to variables. This plugin owns its listener; do not add an ordinary TCP or serial channel.

Gateway device and listener

  1. Open Development Configuration → Collection Configuration → Add Device, choose MQTT Collection Server, and name it Collect_MQTT_Server. Choose a free port; two devices or brokers cannot bind the same endpoint.

Create an MQTT collection server device

  1. In Plugin Properties, select Tcp and port 18883. MQTTX on the same computer uses 127.0.0.1:18883. Remote terminals use a reachable gateway interface IP with the listener allowed through the firewall. The Web management port is separate.

MQTT server listener, port, WebSocket path, and TLS options

Enter 18883 in the listener port field on the right:

MQTT server listener port 18883

  1. Optionally set Allowed Client ID prefix to collect-. Connecting clients then need IDs beginning with it. The example leaves credentials empty and SSL disabled; configure credentials or the server certificate to match the deployment. Save and enable the device.

Allowed client prefix collect- and QoS 0

MQTTX publisher

  1. Create Collect_MQTTX in MQTTX: scheme mqtt://, host 127.0.0.1, port 18883, Client ID collect-mqttx, MQTT 3.1.1, Clean Session enabled. Match account and SSL/TLS settings to the gateway, then connect.

MQTTX gateway endpoint and client identity matching the allowed prefix

Open the complete MQTTX connection window. Across computers, change the MQTTX host to the gateway IP: 127.0.0.1 always refers to the computer running that program.

  1. Open Protocol Debug → Status Monitor on the gateway and locate collect-mqttx in the client list. Its remote port is the client’s temporary source port, not the gateway listening port.

MQTT server client list showing collect-mqttx and its remote endpoint

Plugin properties

Listener and authentication

SettingDescription
Connection typeDefault Tcp for native MQTT clients. Choose WebSocket for MQTT over WebSocket clients. Both ends must use the same transport.
PortDefault 1883. Local listening port, 18883 in the example. Use a free port reachable by the clients.
WebSocket pathDefault /mqtt; used only for WebSocket. Clients use the same port and path, with wss:// when SSL is enabled. TCP does not use it.
Enable SSLDisabled by default. Enables TLS on the listener. Clients must use SSL/TLS or WSS and trust the gateway certificate.
Server certificateSelect a server certificate with its private key from Certificate Management when SSL is enabled. The client connection host should match the certificate.
CA certificateCA used to validate client certificates. Mutual TLS also requires the terminal’s client certificate and private key.
Allow untrusted certificateEnabled by default. Disabling it also requires a valid client certificate. Configure the CA and terminal identity together.
SSL protocolDefault System Default negotiates through the operating system when SSL is enabled. Explicit versions require terminal and OS support. Legacy Default is the SSL 3.0/TLS 1.0 combination.
Check certificate revocationDisabled by default. Enabling requires access to the relevant certificate status service.
Allowed Client ID prefixEmpty by default, imposing no prefix restriction. collect- accepts collect-mqttx but rejects sensor-1. This is a starting-string match, not a topic filter; + and # do not act as wildcards here.
UsernameEmpty by default. A non-empty value enables shared-account validation; clients must send the exact username and matching password.
PasswordEmpty by default. Validated with Username. Certificates, credentials, and the Client ID prefix are independent requirements when configured.

Messages, diagnostics, and missing samples

SettingDescription
QoSDefault 0, used for server publishes and RPC control messages. 0: at most once; 1: at least once; 2: exactly once. Broker and subscriber settings determine effective delivery.
Subscription publish concurrencyIn versions exposing this option, default 1, range 1–2147483647. Limits concurrent sends per subscribing connection. Keep 1 for ordering; larger values may change arrival order. It does not control JSON collection processing concurrency.
Detailed logDisabled by default. Enable for connection or message diagnosis; disable afterward to reduce logging.
Check and cleanup intervalDefault 60000, in seconds. Readable variables become offline after this time without a sample and recover on a new valid message. Set the application’s permitted missing-sample duration; this is not disk cleanup.

Create a variable and receive JSON

  1. Add MQTT_Temperature to the server device. Choose Int16, Read only, and array length 1 for one integer. Use a floating-point type for decimals or Object for a complete object. Array length is not the number of topics; PLC word order and byte offsets do not apply.

Server variable device, address, type, and read permission

Variable device, enabled state, and array length 1

  1. Open Variable Address → Edit fields. Enable Subscribe topic = telemetry/line1 and JSONPath = $.data.value. Leave explicit null, condition, publish, and RPC fields unchecked. Click Confirm and fill, then save.

Server topic and JSONPath mapping in the address editor

The address is SubscribeTopic=telemetry/line1;Path=$.data.value;. Here the subscribe topic defines a collection filter; a publisher does not need to subscribe to that topic before reporting data.

  1. In the MQTTX send area, choose JSON, topic telemetry/line1, QoS 0, and Retain disabled. Enter the following payload and click the green arrow at the bottom right to send:
{"data":{"value":31}}

MQTTX JSON format, QoS, Retain, topic, payload and send button

MQTTX published telemetry/line1 message with value 31

  1. Open Live Variables → Details. Compare current value 31, raw value, address, and collection time. Change the value and publish again to check continuing updates.

MQTT server JSON mapping producing variable value 31

To publish from the gateway to subscribers, use Protocol Debug → Publish Message. Have a receiving client subscribe to telemetry/line1, enter that topic and {"data":{"value":32}}, select QoS 0, disable Retain, and publish. The receiving client should see 32. Outbound server publishing and inbound collection are separate directions; publishing does not insert the same message into the server’s own collection variable.

Server publishing telemetry/line1 JSON to subscribing clients

Write a variable and send commands to clients

Inbound JSON updates readable variables. Writing a command variable publishes a message to subscribers. Subscribe in MQTTX first, then use the variable’s Write action to check the actual topic, body, and direction.

  1. In connected MQTTX, click New Subscription, enter commands/line1/#, choose QoS 0, and confirm.

MQTTX subscribing to gateway command topics under commands/line1/#

  1. Add MQTT_Server_Command under the server device: Int16, Write only, array length 1. Keep the existing temperature variable for inbound telemetry.

Server command variable name, Int16 type, and write-only permission

Variable device, enabled state, and array length 1

  1. In Edit fields, enable Publish topic = commands/line1/server-setpoint, Payload mode = Json, and the Retain property with its switch off. Leave subscribe topic, JSONPath, response topic, and timeout unchecked. Click Confirm and fill.

Server publish topic, Json mode, and non-retained command

The resulting address is:

PublishTopic=commands/line1/server-setpoint;PayloadMode=Json;Retain=false
  1. Enable RPC write in the variable’s RPC Configuration and save. It permits variable writes; Response topic in the address controls waiting for an MQTT acknowledgement and is not needed for ordinary publishing. The switch is in the same location as in the client write steps.

RPC write permission enabled and remote-control insurance disabled

  1. Click Write on MQTT_Server_Command in Live Variables. Enter 43, check that it is recognized as a number, and submit.

Writing the number 43 to MQTT_Server_Command

  1. In MQTTX, find commands/line1/server-setpoint with the JSON number 43. The other message, commands/line1/setpoint with 42, belongs to the separate client command example. Match each by its concrete topic. A server variable write sends to subscribing clients and does not automatically update the temperature variable.

MQTTX receiving the gateway's server-setpoint 43 message

The written value is the message body: 43 is not automatically wrapped in a data object. For {"setpoint":43}, use Object with Json and write that object. For unquoted RUN, use String with RawString and write RUN. See the client payload and acknowledgement steps for types, Retain, and {RequestId} responses. The server follows the same address rules and publishes through its listener to connected subscribers.

Address rule

Use PropertyName=Value; with properties in any order. Names are case-insensitive and unused properties can be omitted:

SubscribeTopic=vendor/device;Path=$.data.temperature;Condition=TelemetryCondition;ConditionParameters={"Minimum":"12.5"};

The properties SubscribeTopic, Path, Condition, ConditionParameters, PublishTopic, PayloadMode, Retain, ResponseTopic, and Timeout follow the client address rules. The server listens on the gateway and can also provide the publishing client’s ID to the condition script.

Click Edit fields beside Variable Address, select a condition script, and fill in its named inputs. Click Confirm and fill, then save the variable. See condition parameter configuration for input types and escaping. Unspecified inputs use script defaults; Topic, MqttTopic, ClientId, and MqttClientId receive actual message context when not explicitly configured.

Existing eight-segment addresses remain supported. Applying changed fields in the editor generates the named format; existing addresses require no manual conversion.

Examples:

UseAddress
Read a JSON valueSubscribeTopic=vendor/device;Path=$.data.temperature;
Condition with parametersSubscribeTopic=vendor/device;Path=$.data.temperature;Condition=TelemetryCondition;ConditionParameters={"Minimum":"12.5"};
Publish a raw writePublishTopic=factory/a/reboot;PayloadMode=RawString;Retain=false;
RPC request and responsePublishTopic=factory/a/command/{RequestId};PayloadMode=Json;Retain=false;ResponseTopic=factory/a/response/{RequestId};Timeout=5000;

Payload paths support $, dot paths such as items.0.value, filters [?()], wildcards [*] and .*, and recursive search $..name. With ||, the first existing candidate path is used. Exact case-sensitive property names are preferred; a unique case-insensitive match is used only when no exact name exists.

A present field with null does not guarantee fallback to the next path. Multiple matches select the first result rather than creating multiple variables. Create a read-only variable at SubscribeTopic=vendor/device;Path=$.data.temperature;, connect an independent client, and publish the following payload; the variable should receive 31 with a new collection time:

{"data":{"temperature":31}}

The condition is a DataTrans script name. Its raw input contains the complete JSON payload; it may also declare Topic, MqttTopic, ClientId, or MqttClientId. A wildcard topic updates the same variable for every matching message, so filter by Topic or ClientId when values must be separated by source.

Publish topics cannot contain + or #; {RequestId} must be the final topic level.

A subscribe topic and JSONPath must be provided together; publish options require a publish topic. RPC response topic and timeout are a pair, and the timeout must be 100–60000 ms. A read-only variable cannot contain publish fields, a write-only variable cannot contain telemetry fields, and every variable must configure at least one route.

Device debug

Open Development Configuration → Collection Configuration, select the MQTT server device, open Protocol Debug at the top of the workspace.

MQTT server protocol panel

Use the panel to inspect clients, subscriptions, and server operations.

Functions

FunctionPurpose
Publish messagePublish a topic, payload, QoS, and Retain flag to subscribing clients.
Recent messagesInspect the 10 most recently active topics and the last 10 received or published messages per topic.
Topic statisticsInspect each topic and its current subscription count.
Client listInspect client ID, username, remote endpoint, and connection time.
Kick clientDisconnect a selected client; confirm that it is safe before using this action.

Connection and messages

  1. Start the MQTT server and confirm its listening port.
  2. Connect a test MQTT client with an approved Client ID and credentials.
  3. Publish one JSON payload to an exact test topic.
  4. Confirm the condition, JSONPath, variable value, and collection time.
  5. Test a write only when the variable and server permissions allow it.

Keep the client connected but stop new telemetry to check the configured missing-sample interval. The variable should become offline and then recover after a new valid message; a connected client alone does not establish data freshness. Verify two clients separately when testing ClientId filtering, and confirm that rejected messages do not replace the accepted value or sample time.

For writes, Json preserves the input JSON type and RawString accepts only a string, publishing its unwrapped text. Enable the variable’s RPC write permission, inspect the message with a subscriber, and verify field feedback. RPC requires the matching RequestId and response within its timeout.

Recent messages

Open Recent messages in Protocol Debug, then expand a topic to read its payloads. Topics are ordered by their latest activity, and messages within a topic are newest first. Each message shows its recorded time, received or published direction, QoS, Retain flag, and payload encoding. Use Refresh to read the current records.

Recent topics and payloads in the MQTT collection server

The current device instance retains up to 10 topics and 10 messages per topic. New messages and topics replace the oldest records. Stopping or reinitializing the device clears them. Client publications and server publications are recorded even without a variable mapping; a server publication is counted once regardless of subscriber count.

Each payload preview is limited to its first 64 KiB. Larger payloads show their full byte length and a truncation notice. Valid UTF-8 is displayed as text, ending at the last complete character if the limit splits a character. Invalid UTF-8 is displayed as Base64. Scroll the message list to browse the remaining records, or scroll a payload independently. With the list focused, Home and End reach its first and last messages. Records load when entering the tab or refreshing manually, without continuous payload polling. These runtime debug records do not replace historical storage or MQTT retained messages.

Troubleshooting

SymptomCheck
Client cannot connectListener port, connection type, firewall, TLS certificate, credentials, allowed Client ID prefix, and client limit.
Message arrives but variable is unchangedExact topic, JSONPath, condition script, payload shape, and variable data type.
Values from different clients are mixedUse exact topics or filter by ClientId in the condition script.
Publish operation failsClient connection, topic permissions, QoS, retain setting, and server log.
Certificate is rejectedServer certificate, CA, client certificate trust, and TLS protocol.