Skip to content
Stand with Ukraine flag

RPC

The MQTT RPC API lets ThingsBoard send commands to devices (server-side RPC) and lets devices call the platform for data (client-side RPC). See Command & Control for the full conceptual overview including persistent RPC and state machine, and Getting Connected for connection parameters and credential types.

ThingsBoard delivers a command to the device. The device optionally replies (two-way) or just receives it (one-way).

FormatSubscribe to incoming commandsReceive on
Shortv2/r/req/+v2/r/req/$request_id
Standardv1/devices/me/rpc/request/+v1/devices/me/rpc/request/$request_id
{"method": "setTemperature", "params": {"value": 26}}

For two-way RPC — publish the response to:

FormatRespond on
Shortv2/r/res/$request_id
Standardv1/devices/me/rpc/response/$request_id

Use the same $request_id from the incoming topic.

Terminal window
# Subscribe to incoming RPC commands
mosquitto_sub -d -q 1 -h "mqtt.eu.thingsboard.cloud" -p 1883 -t "v2/r/req/+" -u "$ACCESS_TOKEN"

The following example is written in JavaScript and is based on mqtt.js. Pure command-line examples are not available because subscribe and publish need to happen in the same MQTT session.

  1. Save the mqtt-js-rpc-from-server.js file to your PC.

    The content of the mqtt-js-rpc-from-server.js file:

    mqtt-js-rpc-from-server.js
    var mqtt = require('mqtt');
    var client = mqtt.connect('mqtt://localhost', {
    username: process.env.TOKEN
    });
    client.on('connect', function () {
    console.log('connected');
    client.subscribe('v1/devices/me/rpc/request/+')
    });
    client.on('message', function (topic, message) {
    console.log('request.topic: ' + topic);
    console.log('request.body: ' + message.toString());
    var requestId = topic.slice('v1/devices/me/rpc/request/'.length);
    //client acts as an echo service
    client.publish('v1/devices/me/rpc/response/' + requestId, message);
    });
  2. Now, follow these steps:

    • Use the RPC debug terminal widget in your ThingsBoard instance.

    • Execute the command to subscribe to RPC commands from the server:

      Terminal window
      export TOKEN=$ACCESS_TOKEN
      node mqtt-js-rpc-from-server.js
    • Send an RPC request connect to the device using the RPC debug terminal widget.

    • You should receive a response from the device.

The device asks ThingsBoard to perform an operation or return data — for example to fetch the current server time or query an external service via a Rule Chain.

FormatSubscribe to responsesPublish request to
Shortv2/r/res/+v2/r/req/$request_id
Standardv1/devices/me/rpc/response/+v1/devices/me/rpc/request/$request_id

ThingsBoard routes the request through the Rule Engine as a TO_SERVER_RPC_REQUEST message and replies on: v2/r/res/$request_id

Request and response payloads:

Request:

{"method": "getCurrentTime", "params": {}}

Response:

{"currentTime": 1735689600000}

A built-in client-side RPC that returns the current rate limits enforced for this device’s session. Useful for self-throttling in firmware.

Request:

{"method": "getSessionLimits", "params": {}}

Response:

{
"maxPayloadSize": 65536,
"maxInflightMessages": 100,
"rateLimits": {
"messages": "200:1,6000:60,14000:3600",
"telemetryMessages": "100:1,3000:60,7000:3600",
"telemetryDataPoints": "200:1,6000:60,14000:3600"
}
}

Rate limit format: count:windowSeconds — e.g. 200:1 means 200 messages per second.