How-to
How to query InfluxDB over MQTT with a bridge flow
Short answer
InfluxDB does not answer queries over MQTT: it serves them over its HTTP API. Put a bridge on the edge PC — a Node-RED flow subscribed to a request topic — that maps a validated, bounded request to a query against the bucket and publishes the result to a separate response topic, so request and reply never loop.
On this page
- Can InfluxDB answer a query over MQTT by itself?
- What hops does a query request take from topic to bucket?
- Which topics should the bridge subscribe and publish to?
- How do you build the bridge flow in Node-RED?
- How big can one request and response safely be?
- How do you test the full request-to-result path?
- What isolates a hop when no response arrives?
- Which design do you commit to?
- Questions asked
InfluxDB has no MQTT query interface: it serves reads over HTTP, so a client that wants query results on a topic needs a bridge in between. A Node-RED flow is that bridge — it subscribes to a request topic, maps the request to a bounded query against the bucket, runs it, and publishes the result to a separate response topic. This walkthrough builds that flow, keeps the two topics apart so the bridge never answers itself, bounds the query so one request cannot pull a month of samples, and tests every hop from published request to parsed result.
Can InfluxDB answer a query over MQTT by itself? #
No. InfluxDB serves queries over HTTP — a Flux request to the /api/v2/query endpoint of the v2 API — and has no listener that answers subscriptions, so MQTT alone never returns bucket data. MQTT is a message transport: a broker forwards publications to whoever subscribed, and that is the whole of it. The question does an MQTT broker store historic data you can query gets the same answer from the other side.
Your writes already work because some component subscribes and pushes points to the /api/v2/write endpoint. Reading is the same pattern reversed: a component subscribes to a request, queries over HTTP, and publishes the result. Build that component once and every MQTT client on the plant network gains a query path.
What hops does a query request take from topic to bucket? #
Four, and each fails differently: requester to broker, broker to bridge, bridge to the bucket over HTTP, then bridge back through the broker to the consumer.
| Hop | From → to | What proves it works |
|---|---|---|
| Request published | Client → broker | The broker's client view shows the publication on the request topic |
| Request delivered | Broker → bridge | The bridge log records receipt, with a subscription matching the exact topic |
| Query run | Bridge → InfluxDB | The bridge reaches the HTTP endpoint and its token authorizes reads on the bucket |
| Result published | Bridge → consumer | The consumer subscribed to the response topic receives and parses the payload |
Test each connection from the component that initiates it. A broker reachable from your engineering workstation says nothing about the route from the bridge host, and addresses, ports and security settings come from the running deployment, never from the topic name.
Which topics should the bridge subscribe and publish to? #
Two different ones: the bridge subscribes to a request topic and publishes results to a separate response topic — never the same topic, or the bridge answers its own output and loops. If the bridge subscribed where it publishes, each response would arrive as a new request and trigger another query until you stopped the flow. Separate topics also keep the request format and the result format from having to share one payload shape.
Illustrative names, since nothing in MQTT or InfluxDB fixes them: plant/influx/query/request for requests and plant/influx/query/response for results. Keep the subscription narrow. An MQTT input node accepts + for one topic level and # for multiple levels, and a subscription widened with # will receive the bridge's own responses, so subscribe to the exact request topic or only the request branch.
From the consumer's side the response topic is an ordinary MQTT feed, so the wiring described in routing MQTT sensor data into Grafana on an edge PC serves a dashboard consumer unchanged. Platforms that already do two-way MQTT follow the same discipline; a bidirectional MQTT publish and subscribe setup shows the same split of publish side and subscribe side on another system.
One rule is not negotiable: no MQTT client ever supplies query text. The request payload selects from a fixed set of measurements, fields and bounded time ranges, and the bridge maps that selection onto the query it owns. Credentials and the access token stay in the bridge's node configuration, never in a payload that transits the broker.
How do you build the bridge flow in Node-RED? #
Six stages in a line: an MQTT input on the request topic, a validate step, a query-build step, the InfluxDB query itself, a format step, and an MQTT output on the response topic.
- Subscribe. Add an MQTT input node, point its broker configuration at your broker, and set the subscription to the exact request topic.
- Validate. In a function node, check the parsed payload against the allowlist: known measurement and field names, numeric or ISO-8601 start and end, a plausible request id. Anything else is rejected with an explicit error published to the response topic.
- Build the query. Map the validated fields onto the fixed query text the flow owns, and clamp the time window and row count here, in code, not in the requester's promise.
- Run it. Send the query to the bucket over the /api/v2/query endpoint with a token scoped to reading that bucket. The connection settings are the ones covered in connecting Node-RED to InfluxDB 2.6 with the v2 API.
- Format. Turn the result into a stable response payload — request id, status, rows — sized and named for the consumer, not for the database. A query that legitimately returns no points still publishes a response marked empty.
- Publish. Feed an MQTT output node set to the response topic. Errors from the query leg get their own status value, so a consumer can tell an authorization failure from an empty window.
An illustrative request the validate step accepts — the field names are ones you define in that step, none of them fixed by MQTT or InfluxDB:
{"id": "q-041", "measurement": "oven_temp", "field": "pv", "start": "2026-01-06T09:00:00Z", "end": "2026-01-06T09:05:00Z"}
and the response that comes back:
{"id": "q-041", "status": "ok", "points": 300}
Agree the response structure with the consumer before building the format step. Raw query output and a compact application structure are both defensible; a mismatch discovered after deployment is not.
How big can one request and response safely be? #
Cap the window at 5 minutes and the rows at what that window can hold: at 1 Hz sampling a 5-minute window holds at most 300 points, about 18 kB as JSON in one message, while an unbounded 30-day pull at the same rate is 2,592,000 rows. Illustrative arithmetic for one tag logged at one sample per second:
- Window: 5 min × 60 s/min = 300 s, so at 1 sample/s the window holds at most 300 points.
- Payload: 300 points × 60 B/point = 18,000 B ≈ 18 kB of JSON in a single message a controller can parse without trouble.
- Unbounded: 30 days × 24 h/day × 3,600 s/h = 2,592,000 s, so 2,592,000 points; at 60 B/point that is 155,520,000 B ≈ 156 MB pushed onto one topic.
The clamp lives in step 3 of the flow, and the requester cannot widen it, because the requester only ever sends parameters. A consumer that needs a longer history pages through consecutive 5-minute windows, matching responses by request id.
How do you test the full request-to-result path? #
Subscribe a test client to the response topic first, then publish four requests in order — a known window with data, a known empty window, an invalid request, and one with the database unreachable — and each must look different on the response topic.
- Known window with known values: the response arrives on the response topic, in the encoding the consumer expects, with values matching the data you wrote.
- Known empty window: a well-formed response marked empty arrives. This proves silence is a fault, not an answer.
- Invalid request — a field off the allowlist, a non-numeric timestamp: an explicit rejection comes back and no query runs.
- Database unreachable or unauthorized: a distinguishable error status, and no repeated re-querying.
Watch the same request at every hop while you do this: the broker's client view, the flow log, and the consumer. The hop where the message stops is the hop that failed.
What isolates a hop when no response arrives? #
Where the request last appears decides it: visible at the broker but absent from the bridge log is a subscription mismatch; a bridge query error is the database leg; a completed query with no consumer message is the publish leg or the consumer's subscription.
| Symptom | Failed hop | What to compare |
|---|---|---|
| Broker shows the publication, bridge log silent | Broker → bridge | Exact topic strings, including case, slashes and wildcard levels |
| Bridge logs receipt, query errors | Bridge → InfluxDB | Route to the host, service state, token scope on the bucket |
| Query completes, consumer sees nothing | Bridge → consumer | Response topic spelling against the consumer's subscription |
If the requester cannot reach the broker at all, work through why a device cannot publish MQTT to a broker on your PC before touching the flow.
Which design do you commit to? #
The bridge, not the workaround: a Node-RED flow on the edge PC, subscribed to the exact request topic, accepting only allowlisted parameters, clamping every window to 5 minutes, and publishing to a separate response topic. Direct HTTP from the controller to the query endpoint is legitimate only when that controller is the sole consumer and the result never needs to be on a topic; the moment a second subscriber wants the data, the bridge is the design.
Questions asked
Can the PLC query InfluxDB directly instead of using MQTT?
Yes. A controller that can make an HTTP request can read the bucket straight from the /api/v2/query endpoint, and that is the lighter path when the controller is the only consumer. The result then exists only inside the controller: it never appears on a topic unless the controller or a bridge also publishes it, so a second consumer still needs the bridge.
Can the bridge run InfluxQL instead of Flux on InfluxDB 2.x?
Yes. InfluxDB 2.x exposes the v1 /query compatibility endpoint, which accepts InfluxQL against v2 buckets, so the bridge can serve either dialect. Fix the dialect and the query template inside the flow; the MQTT request only ever chooses parameters such as measurement, field and time range, never the query language itself.
How do two consumers tell their responses apart?
Echo a request identifier. The requester puts an arbitrary id field in the request payload, the bridge copies it verbatim into the response, and each consumer discards responses whose id it did not issue. Timestamps order the points inside one response; they do not establish ownership of the response itself.
Should query responses be published as retained MQTT messages?
No. A retained response would hand the last query's result to every late subscriber as if it were fresh, and unrelated requests share the topic. Publish responses unretained and let the request identifier carry currency; a consumer that needs a value at startup sends a request.