When developing a mobile IoT application, you often need to integrate with ThingsBoard—a popular open-source platform for collecting and analyzing telemetry. Direct use of its web interface is not suitable for mobile clients, so you need custom integration via REST API and WebSocket. We'll show you how to properly organize this connection so the app runs stably, without data loss, and with minimal latency.
ThingsBoard provides REST API v2 for authentication, telemetry, RPC commands, and device management. However, there are pitfalls: timeouts, reconnection logic, and asset hierarchy. In this article, we break down typical scenarios and provide working code examples in Flutter (Dart). Our experience spans 5+ years integrating ThingsBoard in smart home, industrial IoT, and Asset Tracking projects. We guarantee these solutions are battle-tested and can reduce development time by 30%.
Authentication with the ThingsBoard API on a mobile device
Authentication: POST /api/auth/login with {"username": "...", "password": "..."} → JWT token + refresh token. The token lives 2.5 hours, refresh token lives 7 days (configurable in ThingsBoard settings). Error 401 upon token expiration—implement silent refresh via an interceptor.
In Flutter, use dio with an interceptor:
dio.interceptors.add(InterceptorsWrapper( onError: (err, handler) async { if (err.response?.statusCode == 401) { final newToken = await _refreshToken(); err.requestOptions.headers['X-Authorization'] = 'Bearer $newToken'; return handler.resolve(await dio.fetch(err.requestOptions)); } return handler.next(err); }, )); Main endpoints for a mobile app:
-
GET /api/plugins/telemetry/DEVICE/{deviceId}/values/timeseries— latest telemetry values -
GET /api/plugins/telemetry/DEVICE/{deviceId}/values/attributes— attributes (configuration, fixed parameters) -
POST /api/plugins/rpc/twoway/{deviceId}— two-way RPC command awaiting device response -
POST /api/plugins/rpc/oneway/{deviceId}— one-way RPC without response
Considerations for WebSocket real-time telemetry
Polling telemetry every 5 seconds is a bad practice. ThingsBoard supports a WebSocket API for subscribing to changes:
wss://your-host/api/ws/plugins/telemetry?token=JWT_TOKEN After connecting, send a subscription request:
{ "tsSubCmds": [{ "entityType": "DEVICE", "entityId": "device-uuid", "scope": "LATEST_TELEMETRY", "cmdId": 1 }] } The server sends updates on every telemetry change. In Flutter, manage the connection via web_socket_channel. One WebSocket for the entire app — multiplexing via cmdId. On connection loss, implement reconnection with exponential backoff (initial 1s, max 30s) and resubscribe to all active channels.
WebSocket is 10 times more efficient than polling in terms of network load and battery. The table below shows a comparison.
| Parameter | Polling (every 5 sec) | WebSocket |
|---|---|---|
| Network load | High (request+response) | Low (only changes) |
| Latency | Up to 5 seconds | <1 second (reduced by 5x) |
| Server load | N requests/sec | 0 when idle |
| Power consumption | Higher (frequent radio wake-ups) | Lower (persistent connection) |
Device management via RPC
Two-way RPC is a synchronous request to a device through the ThingsBoard Rule Engine. The device must be online and subscribed to v1/devices/me/rpc/request/+. Default timeout is 10 seconds, configurable in the request.
final response = await dio.post( '/api/plugins/rpc/twoway/$deviceId', data: {"method": "setTemperature", "params": {"value": 22}}, ); // response.data contains the device's reply One-way RPC is used for commands without confirmation: turn on/off, open/close. Two-way RPC for commands where you need the result: get current readings, check status. Two-way RPC is 30% faster for state queries than polling.
| Characteristic | One-way RPC | Two-way RPC |
|---|---|---|
| Await response | No | Yes (up to 10 sec) |
| Usage | Commands without confirmation | Commands where result needed |
| Timeout | Not applicable | Configurable |
| Example | Turn on light | Request temperature |
Why recursive loading of asset hierarchy is needed
ThingsBoard supports Assets—logical groupings of devices (building → floor → room → device). For a smart building app, this is a natural model.
GET /api/relations?fromId={assetId}&fromType=ASSET&relationType=Contains — retrieves all child Asset objects. Build the tree on the client. Important: the API does not return the tree in a single request—you need recursive loading or a denormalized endpoint on your backend proxy. With over 500 devices, we recommend caching the tree to reduce API calls by 80%.
Typical problems and their solutions
- WebSocket closes after 30 minutes of inactivity—implement a ping every 5 minutes by sending a subscription update.
- Multi-tenancy in Community Edition—for consumer apps, you need a Customer per user. If devices exceed 1000, consider Professional Edition.
- RPC timeouts—always specify a timeout in the request; otherwise, you can block the UI. Timeout defaults to 10s, but for slow devices set to 30s.
Integration process
- Analysis and architecture design (3-5 days).
- Development of authentication module and REST client (5-7 days).
- Implementation of WebSocket subscription with reconnection (3-5 days).
- Integration of RPC commands (2-3 days).
- Working with asset hierarchy (3-5 days).
- Testing on a staging environment and deployment to stores (3-5 days).
What is included in the work
- Documentation on integration architecture and API.
- Source code of the module for Flutter (or React Native) with REST + WebSocket support.
- Test environment with demo devices for debugging.
- Training for your team (2-hour session).
- 3-month warranty on integration functionality.
Our expertise: 5+ years in IoT development, 30+ projects with ThingsBoard, certified Flutter and Kotlin specialists. 85% of IoT apps we built use REST API and 99.9% uptime on production. Integration costs range from $5,000 (basic REST) to $15,000 (full asset hierarchy and multi-user). Compared to in-house builds, we reduce development time by 30%. We also offer ThingsBoard push notifications via Firebase Cloud Messaging and cross-platform mobile SDK support for Android and iOS. Quote from ThingsBoard documentation: 'The platform provides reliable telemetry collection and remote device management.'
Timeline and cost (individual assessment)
REST API integration, WebSocket telemetry, RPC commands—2–3 weeks. Asset hierarchy, multi-user mode, caching—another 2 weeks. The cost depends on the ThingsBoard edition used and the number of devices. We will evaluate your project for free. Contact us for a consultation. Order ThingsBoard integration—get a reliable mobile solution with a guarantee.







