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.
Server-Side RPC
Section titled “Server-Side RPC”ThingsBoard delivers a command to the device. The device optionally replies (two-way) or just receives it (one-way).
| Format | Subscribe to incoming commands | Receive on |
|---|---|---|
| Short | v2/r/req/+ | v2/r/req/$request_id |
| Standard | v1/devices/me/rpc/request/+ | v1/devices/me/rpc/request/$request_id |
{"method": "setTemperature", "params": {"value": 26}}For two-way RPC — publish the response to:
| Format | Respond on |
|---|---|
| Short | v2/r/res/$request_id |
| Standard | v1/devices/me/rpc/response/$request_id |
Use the same $request_id from the incoming topic.
# Subscribe to incoming RPC commandsmosquitto_sub -d -q 1 -h "mqtt.thingsboard.cloud" -p 1883 -t "v2/r/req/+" -u "$ACCESS_TOKEN"Example
Section titled “Example”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.
-
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 serviceclient.publish('v1/devices/me/rpc/response/' + requestId, message);}); -
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_TOKENnode 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.
-
Client-Side RPC
Section titled “Client-Side RPC”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.
| Format | Subscribe to responses | Publish request to |
|---|---|---|
| Short | v2/r/res/+ | v2/r/req/$request_id |
| Standard | v1/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}When the device profile payload type is set to Protobuf, or when using the short Protobuf topics, the following fixed schemas apply. These schemas cannot be customized in the device profile.
Request — short Protobuf topic: v2/r/req/$request_id/p
syntax = "proto3";
message RpcRequest { string method = 1; string params = 2;}Response — short Protobuf topic: v2/r/res/+/p
syntax = "proto3";
message ToServerRpcResponseMsg { int32 requestId = 1; string payload = 2; string error = 3;}Get Session Limits
Section titled “Get Session Limits”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.