Explainer
MQTT client ID device versus application roles explained
Short answer
One MQTT client ID identifies exactly one connection, and a broker must disconnect an established client the moment another connects with its ID. A publishing edge device therefore connects with a device-form ID such as d:orgId:deviceType:deviceId, while a cloud subscriber connects as an application with a:orgId:appId. Give every node its own client ID; the specialized IoT input connects because it builds that identity for you.
On this page
- What the broker does with a client ID
- The device form: d:orgId:deviceType:deviceId
- The application form: why the specialized input connects and the generic one does not
- What a client ID collision looks like
- Device or gateway: which role is this publisher
- Worked example: two client IDs for organization abc123
- Getting the generic MQTT input connected
- Questions asked
An MQTT client ID is the identity of exactly one connection on one broker, and the protocol specification requires the broker to disconnect an established client the moment a second one connects with that same ID. On a plant setup where a Node-RED publisher on a Raspberry Pi feeds events to an MQTT platform and a second Node-RED instance in the cloud subscribes to them, that one rule gives the two sides different roles: the Pi connects as a device, the cloud subscriber connects as an application. It also explains the familiar symptom in which the platform's specialized IoT input connects while a generic MQTT input pointed at the same broker does not. The specialized node assembles the whole identity for you; the generic node hands you empty fields, and a single missing field is enough to keep the connection out.
What the broker does with a client ID #
The client ID is the connection's handle, not a password, and three documented behaviors hang on it.
Uniqueness: the MQTT 3.1.1 specification says that when a new connection arrives carrying a client ID that a connected client is already using, the server must disconnect the existing client. The rule exists so that a client recovering from a network drop replaces its own stale connection instead of doubling it.
Reconnect handling: brokers use the ID to recognize a reconnecting client as the same one and to clean up the old session behind it.
Authorization: access rules can be keyed on identity. The documented broker authorization for Azure IoT Operations, for example, grants rights by named client IDs — its example allows temperature-sensor and humidity-sensor. On a broker you run yourself the same applies: the Run a secure Mosquitto MQTT lab broker with Node-RED setup gives every client its own identity and its own access rules.
The decision that follows is arithmetic, not judgment: count the simultaneous MQTT connections your system holds and provision that many distinct client IDs, one per node, before anything else.
The device form: d:orgId:deviceType:deviceId #
For the IBM Watson IoT Platform, the documented device client ID has four colon-separated fields: d:orgId:deviceType:deviceId. The organization ID is assigned by the platform; the device type and the device ID are the values under which the device is registered. The documented connection example pairs that identity with TLS on port 8883, username and password authentication, and events published under the iot-2 topic space.
A publisher that reads its own sensors and pushes its own events is exactly this case. On the Pi publisher in this setup, the reported change that made the data appear in the platform was the complete device-form client ID. Without it the platform had no registered device to attach the events to, so the data stayed invisible.
The application form: why the specialized input connects and the generic one does not #
The cloud Node-RED subscriber is not a device; it consumes events, so it takes the other role, the application. The platform's specialized IBM IoT input connects because it builds the complete application identity from the connection you created between the platform and Node-RED — organization, credentials, client ID and TLS all supplied for you. A generic MQTT input presents the same blanks with nothing behind them, and an incomplete identity is the whole reason it fails.
The application client ID follows the same role-letter convention but carries one identifier instead of type plus ID — the form reported in working setups as a:orgId:appId. The credentials that go with it are the API key and token that the platform-to-Node-RED connection created: the key as the username, the token as the password.
When the specialized input connects and the generic one does not, put both configurations side by side and compare field by field: organization, username, password, client ID, TLS. The specialized node's own configuration shows a complete, working application identity, and the field that differs from what you typed is the failing one. The symptom — events visible at the platform but absent in the application — is the same class as cloud broker data that never reaches SCADA tags, worked through in Fixing MQTT cloud broker data not reaching SCADA tags.
What a client ID collision looks like #
A cloud instance often ends up running two MQTT inputs at once: the specialized IoT input you proved works and the generic input you are migrating to. Those are two connections. Give them the same client ID and the broker disconnects the established client each time the other connects, the displaced client reconnects, and the pair fights — inputs flap between connecting and disconnected while subscriptions come and go.
The same trap hides in copied flows: a Node-RED flow exported with its client ID baked in carries that identity into every instance that imports it. Change the ID in each imported copy before the flow goes live.
Keep the cloud subscriber on the event topics it consumes. Command topics that can move equipment belong on their own connection with their own identity, which is the separation set out in Node-RED IO data MQTT setup: keep publish and control separate.
Decision: one client ID per node across both instances, no exceptions.
Device or gateway: which role is this publisher #
A device connection speaks for itself: one endpoint publishing its own measurements. A gateway connection speaks for others: one box that represents downstream devices and forwards their data. The Pi reading its own GPIO inputs for alarms, runtime and cycle counts is publishing its own measurements, so it takes the device role and the device-form identity above.
Reserve the gateway role for the case where one edge box must speak for several devices that cannot connect on their own; the trade-offs of publishing directly versus forwarding through a gateway are compared in PLC MQTT direct publishing versus an IoT gateway. Settle the role before you touch the client ID, because changing the role changes the identity the platform expects.
Worked example: two client IDs for organization abc123 #
Inputs, each with its origin: organization ID abc123, the example organization the platform documentation itself uses in its client-ID examples; device type S7-1200-PLC and device ID 001BEB123456, the same documented example device, so every value here is traceable and your own registration takes those same two slots; application identifier edge-subscriber, a name for the application connection, given here as an example value.
The rule, in words: the device client ID is the role letter d, then the organization ID, then the registered device type, then the registered device ID, joined by colons; the application client ID is the role letter a, then the organization ID, then the application identifier. A broker admits as many simultaneous connections as there are distinct client IDs, and two clients sharing one ID means the established connection is disconnected the moment the second one connects.
The arithmetic:
d:+abc123+:+S7-1200-PLC+:+001BEB123456=d:abc123:S7-1200-PLC:001BEB123456— four fields.a:+abc123+:+edge-subscriber=a:abc123:edge-subscriber— three fields.
Distinct-ID count: the two strings differ, so a publisher holding the first and a subscriber holding the second are two distinct identities, and both connections stand at the same time.
Counter-case: set both nodes to the same device string from the first line and the distinct-ID count is one across two nodes. The second CONNECT forces the broker to drop the established client, the dropped client reconnects, and the two lock into a connect-and-drop loop.
Result: two distinct identity strings, one per role, hold two stable connections against one broker.
Decision: enter the device string only in the Pi's MQTT out node, enter the application string only in the cloud MQTT in node, and reuse neither string in any other node in either instance. The threshold is exact: one shared client ID between two live nodes is already a fault.
Getting the generic MQTT input connected #
Five values decide it:
- Server: your organization ID prepended to messaging.internetofthings.ibmcloud.com — for example abc123.messaging.internetofthings.ibmcloud.com.
- TLS enabled on port 8883, the port the documented connection example uses. A TLS endpoint and a plain connection never finish a handshake, and a socket that opens while the MQTT handshake stalls is the failing hop traced step by step in MQTT TLS connects but no CONNACK: find the failing hop.
- Username: the API key created by the connection between the platform and Node-RED.
- Password: that key's token.
- Client ID: the application-form string from the worked example above.
Then test at the message boundary rather than at the connection indicator: inject a fresh event at the Pi, confirm it appears in the platform, then confirm the same event arrives at the cloud input. An event that stops at the platform points at the publisher side; an event that never leaves the Pi points there first; an event that reaches both ends closes the loop.
The documented path authenticates with username and password over TLS, with no client certificate in that path. Start there, and add a client certificate only where your account's platform configuration actually requires one — a missing certificate shows up as a stalled handshake, not as a rejected client ID, so the symptom tells you which layer failed.
Questions asked
Why did adding d: to the client ID make the Pi's data appear in the platform?
The d: prefix marks the connection as a device connection, and the fields behind it — organization, device type, device ID — tell the platform which registered device the events belong to. A client ID without that device form gives the platform no device to attach the events to, so nothing shows under the device even when the connection itself is accepted.
What goes in the username and password for the cloud subscriber?
The API key and token that the connection between the IoT platform and Node-RED created: the key is the username and its token is the password. The device publisher authenticates differently, as the device itself with its own token, so do not copy the publisher's credentials into the subscriber.
Can two nodes share one client ID if they are never connected at the same time?
Only if they truly never overlap, which a reboot, a redeploy or a stuck flow breaks in practice. The moment both connect, the broker must disconnect the established client and the two keep displacing each other. Give every node in every Node-RED instance its own client ID and treat any shared ID as a fault.
Where does the application identifier in a:orgId:appId come from?
It is the application identity that the API key represents, the application you registered with the platform when you created the connection to Node-RED. The organization field is fixed by your account, the application field must match that registered application, and the API key with its token are the credentials that pair with it. The specialized IoT input's configuration shows a complete working example of all three fields.
Is a certificate needed to connect to the platform?
The documented connection path authenticates with a username and password over TLS on port 8883, with no client certificate in that path. Start there. If your account's platform configuration does require a client certificate, the failure appears as a handshake that stalls rather than as a rejected client ID, which tells you which layer to work on.