Send data to Zerobus Ingest from Node-RED
This feature is in Beta. To use it, a workspace admin must turn on Zerobus Ingest MQTT Endpoint from the Previews page. See Manage Databricks previews.
Node-RED is an open-source, flow-based tool for wiring devices, APIs, and services together. Its built-in mqtt out node publishes MQTT 5 messages. For the node's options, see the Node-RED nodes reference.
Maintained by: the Node-RED open-source project, in the node-red/node-red repository.
Node-RED publishes each message to the Zerobus Ingest MQTT endpoint, which writes the message as one row in a Unity Catalog Delta table. Your flow fetches a table-scoped OAuth token, sends it in the MQTT CONNECT, and keeps the connection alive. You don't need to run a message bus between Node-RED and the lakehouse.
The target table needs a rescue column. Without it, one record that doesn't match the table closes the whole MQTT connection. See Step 5.
When to use Node-RED
Use this path when your data already flows through Node-RED, for example from sensors, device gateways, or HTTP and MQTT inputs that Node-RED reads and reshapes. Node-RED sends each message as a JSON object that matches the target table's columns.
This page covers publishing only. The Zerobus Ingest MQTT endpoint doesn't support subscriptions, so the mqtt in node can't read from it.
How it works
The example flow has four parts:
- Token: an
http requestnode posts an OAuth client credentials request to your workspace's/oidc/v1/token, scoped to the target table. - Connect: a function node turns the token into a connect action for the
mqtt outnode. The token goes in theAuthorizationuser property, and the table name goes inx-databricks-zerobus-table-name. - Publish: the
mqtt outnode sends each record at QoS 1. - Keep the link up: a timer fetches a new token and reconnects if the connection is down. Every 15 minutes, the flow also fetches a new token and reconnects with it.
The mqtt out node doesn't queue records or report a failure to the flow, so records sent while the connection is down are lost. See Step 4.
Requirements
- Node-RED with the built-in
mqtt outnode. The example runs the officialnodered/node-redDocker image. - Docker, to run the image as the example does.
- MQTT enabled for your workspace. See Prerequisites.
- A workspace in a region that supports Zerobus Ingest. MQTT availability can differ from general Zerobus Ingest availability. See Prerequisites and Availability.
- A service principal with an OAuth secret. See Configure authentication. Note the service principal's client ID and OAuth secret.
- A target table that meets the Table requirements, with nullable columns and a
rescue VARIANTcolumn with thezerobus-rescuetag. The rescue column is required. Step 1 creates the table. - Outbound TLS connectivity from the Node-RED host to your Zerobus Ingest hostname on port
8883. The MQTT endpoint doesn't support front-end Private Link, so connect over the public endpoint. See Front-end Private Service Connect.
Create the target table and grant access
Create a table whose columns match the JSON fields your flow sends, plus a rescue column. Keep the columns nullable. Zerobus Ingest stores a value whose type doesn't match its column in rescue only when that column is nullable. Replace <catalog>.<schema>.<table> with your table name. This example uses a dice-roll record:
CREATE TABLE IF NOT EXISTS <catalog>.<schema>.<table> (
roll_id BIGINT,
player STRING,
die_sides INT,
roll INT,
rolled_at STRING,
rescue VARIANT
);
ALTER TABLE <catalog>.<schema>.<table>
ALTER COLUMN rescue SET TAGS ('zerobus-rescue');
A tag change can take up to 5 minutes to take effect, so wait 5 minutes after you apply the tag before you start the flow.
Grant the service principal the privileges that the token request asks for: USE CATALOG on the catalog, USE SCHEMA on the schema, and SELECT and MODIFY on the table. See Configure authentication.
Get your endpoints
Find your workspace URL, workspace ID, and region. See Get your workspace URL and Zerobus Ingest endpoint. The MQTT hostname uses this format, on port 8883:
<workspace-id>.zerobus.<region>.gcp.databricks.com
Start Node-RED with the example flow
The Node-RED MQTT example contains the flow, the Node-RED settings, a configuration template, and scripts/run-docker.sh, which copies the flow files and starts the container. The flow reads every value from an environment variable, so nothing secret is stored in the flow file.
-
Copy the flow and the settings into a
datadirectory. Node-RED reads both from/dataand writes its runtime files there:Bashmkdir -p datacp node-red/flows.json node-red/settings.js data/The container runs Node-RED as UID
1000. On a Linux host, thedatadirectory must be owned by UID1000, or Node-RED can't write its runtime files to it. If your user isn't UID1000, runsudo chown -R 1000:1000 databefore you start the container. See the Node-RED Docker documentation. -
Export the following environment variables.
config.example.envin the example describes each one:Variable
Value
DATABRICKS_WORKSPACE_URLYour workspace URL,
<workspace-url>, including itshttps://prefix.DATABRICKS_WORKSPACE_IDYour numeric workspace ID,
<workspace-id>.DATABRICKS_CLIENT_IDThe service principal's client ID.
DATABRICKS_CLIENT_SECRETThe service principal's OAuth secret.
DATABRICKS_TABLE_NAMEThe table from Step 1,
<catalog>.<schema>.<table>.ZEROBUS_MQTT_HOSTThe MQTT hostname from Step 2, without
https://.PORTA free port for the Node-RED editor.
-
Start the container. The
-eflags without values pass the variables from your shell, and-ppublishes the editor on127.0.0.1only:Bashdocker run -d --name zerobus-node-red \-e PORT -e DATABRICKS_WORKSPACE_URL -e DATABRICKS_WORKSPACE_ID \-e DATABRICKS_CLIENT_ID -e DATABRICKS_CLIENT_SECRET \-e DATABRICKS_TABLE_NAME -e ZEROBUS_MQTT_HOST \-p "127.0.0.1:${PORT}:${PORT}" \-v "$(pwd)/data:/data" \nodered/node-red:5.0.10Function nodes can read environment variables, including the client secret. Binding the editor to
127.0.0.1blocks remote access but not other users on the same host, and the example's editor has no login. Run the container on a single-user host, and for anything beyond a local test, enableadminAuthinsettings.js. See Securing Node-RED. -
Check the container log for these lines, in order:
token … fetched (expires_in 3600s): the token request succeeded. The log shows a short fingerprint of the token, not the token.Connected to broker: … :8883: theCONNECTsucceeded.
To show those lines, run the following:
Bashdocker logs zerobus-node-red | grep -E "token|Connected to broker" -
Query the table. The example publishes one record every 2 seconds, and Zerobus Ingest writes rows asynchronously, so check after a minute or two:
SQLSELECT count(*) AS n, max(rolled_at) AS last FROM <catalog>.<schema>.<table>;The count is above zero and grows as the flow runs.
To open the editor, go to http://127.0.0.1:<PORT>, where <PORT> is the PORT value you set.
Keep the connection alive and refresh the token
The example flow has three timers:
- Start: runs at startup. It fetches a token, and then connects.
- Refresh: runs every 15 minutes. It fetches a new token, and then sends a connect action with
forceset, which closes the connection and opens a new one with the new token. - Watch: runs every 30 seconds. If the connection is up, it does nothing. If the connection is down, it fetches a new token, and then reconnects, so every reconnect carries a new token.
The example sets the MQTT client's automatic reconnect interval (mqttReconnectTime in settings.js) to 2147483647 ms, the largest delay that Node.js timers accept (about 24.8 days), so the client doesn't reconnect on its own within that window. An automatic reconnect would reuse the token from the original CONNECT, so the watch timer is the only reconnect path. Don't set this value to 0: Node-RED replaces 0 with 5000 ms, and the client then reconnects after 5 seconds with the old token.
Records sent while the connection is down are dropped without an error. The watch timer limits how long the connection stays down, but it doesn't prevent the loss, and a record sent during the short reconnect of a 15-minute refresh can also be dropped. An application that must keep every record needs its own buffer and retry.
Handle records that don't match the table
The mqtt out node doesn't pass a per-record result to the flow. The following table shows what happens to a record that doesn't match the table:
Record | With the rescue column | Without the rescue column |
|---|---|---|
A value with the wrong type, such as the string | The row is stored with | The server sends |
A field that the table doesn't have | The field is stored in | The server sends |
A payload that isn't JSON | The server refuses it with | The server refuses it with |
Without the rescue column, the watch timer reconnects the flow after a bad record, but the records sent during that gap are lost.
Troubleshooting
Symptom | Cause | Fix |
|---|---|---|
| The client ID or secret is wrong, or the service principal can't obtain a token for the table | Check |
No | The hostname or port is wrong, MQTT isn't enabled for the workspace, or outbound TLS to port | Check |
Rows don't appear in the table | Zerobus Ingest writes rows asynchronously, or the table name in | Wait a minute or two, then check the table name and the grants. |
The log shows | A record doesn't match the table, and the table has no column with the | Add the rescue column as Step 1 shows, and wait 5 minutes after you apply the tag. See Step 5. |
The log shows | The payload isn't JSON | Send one JSON object per message. |
Records are missing after a connection drop | Records sent while the connection was down are dropped without an error | Keep the watch timer running, and retry or buffer records in your own flow if you need every record. See Step 4. |
Additional resources
- Use MQTT with Zerobus Ingest: the MQTT endpoint's connection model, authentication, limits, and a Python example.
- Node-RED example flow: the flow, settings, configuration template, and tests.
- Node-RED documentation: running Node-RED, the flow editor, and the Docker image.