IMD SolutionComputing LibraryShop main store

Explainer

ThingsBoard concurrent MQTT sessions per device explained

Edge computing and IIoT Published 4 min read

Short answer

ThingsBoard admits one concurrent MQTT session per device token by default, so a second client such as the IoT Gateway displaces the first, for example a Node-RED telemetry publisher. Set ACTORS_MAX_CONCURRENT_SESSION_PER_DEVICE to 2, keep the client identifiers distinct, and let only one session subscribe to RPC or attribute updates.

On this page
  1. Why one token admits one session
  2. The arithmetic for two clients
  3. Distinct client identifiers are still required
  4. One session owns RPC and attribute updates
  5. Keep or retire the two-device workaround
  6. The decision for each case
  7. Questions asked

One access token admits one MQTT session on ThingsBoard by default. When a second client authenticates with the same token — an IoT Gateway starting next to a Node-RED telemetry publisher on the same Raspberry Pi — the server accepts the new session and drops the old one, which is why the gateway's connect interrupts Node-RED. The control is the server parameter ACTORS_MAX_CONCURRENT_SESSION_PER_DEVICE. Set it to 2, give each client its own MQTT client identifier, and keep RPC and attribute subscriptions in exactly one of the two sessions. That retires the two-device workaround without editing any source code.

Why one token admits one session #

The access token authenticates a device identity, and the server counts admitted sessions per device, not per client identifier. The ceiling is ACTORS_MAX_CONCURRENT_SESSION_PER_DEVICE in application/src/main/resources/thingsboard.yml: the line takes its value from an environment variable of the same name and falls back to 1, and the file's own comment marks it as the maximum number of concurrent sessions per device. A different client identifier does not change that count. A reported case on the project's issue tracker describes a second connection with a different client identifier on the same token still knocking the first device offline. The disconnect you see when both applications connect is admission control working as configured, not a broken broker.

The arithmetic for two clients #

Count the sessions that must stay up simultaneously and hold that count against the limit. The telemetry publisher needs one session and the gateway needs one, so the requirement is 1 + 1 = 2 sessions. That count exceeds the default of 1, and the server keeps whichever session authenticated last; after the change to 2 the count no longer exceeds the limit and both stay connected. Apply the value through the deployment's own configuration mechanism — environment variable or configuration file — and restart the service during a planned interruption. The limit is a plain integer, so at this size the constraint is ownership, not capacity.

Distinct client identifiers are still required #

The token answers who the device is; the MQTT client identifier names the connection, and one identity split across two applications needs both to be right. A duplicated identifier displaces a session at the protocol level regardless of the per-device limit: a reported reconnect loop, with a client dropping roughly every 15 seconds, ended only when each connection was given a unique identifier built from the device's MAC address. Give Node-RED and the gateway different identifiers before the simultaneous test, because a raised limit cannot admit two connections the broker treats as the same session. The split between device versus application roles in MQTT client identifiers also decides how far one identifier should reach across a fleet.

One session owns RPC and attribute updates #

The same tracker answer that documents the parameter discourages raising it when both device sessions subscribe to RPC or attribute updates, and the reason is ownership: two subscribers receive the same request, both may act on it, and the platform then sees competing responses or conflicting local state. Commit the roles before commissioning. Keep Node-RED on telemetry and make the gateway the RPC handler — the arrangement that got RPC Remote Shell working in the first place — which follows the same logic as keeping publish and control separate in a Node-RED MQTT setup. Audit both applications for request subscriptions instead of trusting the flow canvas: an MQTT Out node creates no subscription, but other nodes in the flow might. RPC Remote Shell executes commands on the host, so the token behind it is the secret to protect: keep it out of shared flows and rotate it when a client is retired. If you later want the gateway gone, Node-RED must gain request subscription, dispatch and response handling — the working pattern to copy is configuring Modbus RPC writes through the gateway from a dashboard — and until that is built and tested, the gateway stays.

Keep or retire the two-device workaround #

The workaround works because two devices carry two tokens, and each token owns its own session allowance; the cost is that one Raspberry Pi lives in the platform as two identities, splitting telemetry, RPC targeting and administration. Retire it once the raised limit is live and both clients hold distinct identifiers. Keep it only when the server cannot be restarted yet, or when the two data paths genuinely need separate owners. Where the second application needs device management rather than a live MQTT session, comparison of HTTP and MQTT architecture for IoT device management shows a path that never opens a second session at all.

The decision for each case #

Case Session limit Decision
Second client is telemetry-only and one session handles RPC Set to 2 Consolidate both clients onto one device token with distinct client identifiers
Both clients must subscribe to RPC or attribute updates Keep the default Do not raise it; keep two devices or redesign so one session owns requests
The server cannot be restarted yet Keep the default Run the two-device workaround and schedule the change
A third permanent client joins later Raise to the new client count Admit it only with an assigned role that owns nothing already owned

Questions asked

How do I keep the raised session limit through a ThingsBoard upgrade?

Set the value as an environment variable on the service or in the container definition instead of editing the packaged configuration file. The packaged file keeps its own fallback, so an upgrade that replaces it cannot revert your override. After upgrading, confirm the effective value in the server startup log before reconnecting both clients.

Does the same session limit apply to the MQTT gateway API?

A reply on the project's issue tracker to a report about horizontally scaled gateway services states that the gateway API has no such session limit and that at least 20 sessions were tested. The limit in this article counts device sessions on one access token, so several gateway instances on the gateway API are a different case from two clients sharing a device token.

Should I raise the limit beyond the client count for headroom?

No. Every admitted session can publish and subscribe as the device, and the single-session default exists to keep one owner per identity. Set the value to the number of clients that must stay connected, and treat any later client as a configuration change with an assigned role rather than spare capacity it can use silently.

Keep reading

  • How-to

    Bridging OPC UA PLC data to MQTT with an edge PC

    Keep the OPC UA server on the PLC side and give MQTT publishing its own role on a separate edge PC that acts as the OPC UA client and MQTT publisher. Commission the two links independently — first a verified tag read, then a verified subscriber message — and only then widen the tag set.

    Edge computing and IIoT

  • Explainer

    Edge gateway Modbus TCP polling over the WAN port

    When a data gateway supports MQTT as a client only, drop the broker hop: configure the Modbus TCP poll to target the device's IP address in the same subnet as the gateway's WAN interface. The gateway opens the TCP connection itself, so polled values keep updating with the internet link down. Verify by pulling the uplink and watching one known register.

    Edge computing and IIoT

  • Comparison

    MQTT broker versus direct TCP from a Node-RED edge device

    Both paths make the edge device open the connection as a client. MQTT is TCP plus a broker — one address at the publisher, TLS on 8883, a unique client ID per client, topic-based delivery — while a direct tcp out to tcp in link is faster to build but pins the publisher to each consumer's address and port. Build direct TCP for one fixed pair; build the broker for anything that grows.

    Edge computing and IIoT

Classic historian Excel add-in on 64-bit Office explained