Skip to main content

Send data to Zerobus Ingest from Node-RED

Beta

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.

info

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 request node 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 out node. The token goes in the Authorization user property, and the table name goes in x-databricks-zerobus-table-name.
  • Publish: the mqtt out node 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 out node. The example runs the official nodered/node-red Docker 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 VARIANT column with the zerobus-rescue tag. 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:

SQL
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.

  1. Copy the flow and the settings into a data directory. Node-RED reads both from /data and writes its runtime files there:

    Bash
    mkdir -p data
    cp node-red/flows.json node-red/settings.js data/

    The container runs Node-RED as UID 1000. On a Linux host, the data directory must be owned by UID 1000, or Node-RED can't write its runtime files to it. If your user isn't UID 1000, run sudo chown -R 1000:1000 data before you start the container. See the Node-RED Docker documentation.

  2. Export the following environment variables. config.example.env in the example describes each one:

    Variable

    Value

    DATABRICKS_WORKSPACE_URL

    Your workspace URL, <workspace-url>, including its https:// prefix.

    DATABRICKS_WORKSPACE_ID

    Your numeric workspace ID, <workspace-id>.

    DATABRICKS_CLIENT_ID

    The service principal's client ID.

    DATABRICKS_CLIENT_SECRET

    The service principal's OAuth secret.

    DATABRICKS_TABLE_NAME

    The table from Step 1, <catalog>.<schema>.<table>.

    ZEROBUS_MQTT_HOST

    The MQTT hostname from Step 2, without https://.

    PORT

    A free port for the Node-RED editor.

    Variable

    Value

    DATABRICKS_WORKSPACE_URL

    Your workspace URL, <workspace-url>, including its https:// prefix.

    DATABRICKS_WORKSPACE_ID

    Your numeric workspace ID, <workspace-id>.

    DATABRICKS_CLIENT_ID

    The service principal's client ID.

    DATABRICKS_CLIENT_SECRET

    The service principal's OAuth secret.

    DATABRICKS_TABLE_NAME

    The table from Step 1, <catalog>.<schema>.<table>.

    ZEROBUS_MQTT_HOST

    The MQTT hostname from Step 2, without https://.

    PORT

    A free port for the Node-RED editor.

  3. Start the container. The -e flags without values pass the variables from your shell, and -p publishes the editor on 127.0.0.1 only:

    Bash
    docker 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.10

    Function nodes can read environment variables, including the client secret. Binding the editor to 127.0.0.1 blocks 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, enable adminAuth in settings.js. See Securing Node-RED.

  4. 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: the CONNECT succeeded.

    To show those lines, run the following:

    Bash
    docker logs zerobus-node-red | grep -E "token|Connected to broker"
  5. Query the table. The example publishes one record every 2 seconds, and Zerobus Ingest writes rows asynchronously, so check after a minute or two:

    SQL
    SELECT 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 force set, 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 six for the INT column roll

The row is stored with roll as NULL, and the value is stored in rescue. The connection stays open.

The server sends DISCONNECT with reason code 131 (0x83, Implementation specific error), and the connection closes. The record isn't stored.

A field that the table doesn't have

The field is stored in rescue. The connection stays open.

The server sends DISCONNECT with reason code 131 (0x83, Implementation specific error), and the connection closes. The record isn't stored.

A payload that isn't JSON

The server refuses it with PUBACK reason code 153 (0x99, Payload format invalid), and Node-RED logs Publish error: Payload format invalid. Nothing is stored. The connection stays open.

The server refuses it with PUBACK reason code 153 (0x99, Payload format invalid), and Node-RED logs Publish error: Payload format invalid. Nothing is stored. The connection stays open.

Record

With the rescue column

Without the rescue column

A value with the wrong type, such as the string six for the INT column roll

The row is stored with roll as NULL, and the value is stored in rescue. The connection stays open.

The server sends DISCONNECT with reason code 131 (0x83, Implementation specific error), and the connection closes. The record isn't stored.

A field that the table doesn't have

The field is stored in rescue. The connection stays open.

The server sends DISCONNECT with reason code 131 (0x83, Implementation specific error), and the connection closes. The record isn't stored.

A payload that isn't JSON

The server refuses it with PUBACK reason code 153 (0x99, Payload format invalid), and Node-RED logs Publish error: Payload format invalid. Nothing is stored. The connection stays open.

The server refuses it with PUBACK reason code 153 (0x99, Payload format invalid), and Node-RED logs Publish error: Payload format invalid. Nothing is stored. The connection stays open.

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

Token request failed with HTTP 400; nothing connected (or 401) in the log

The client ID or secret is wrong, or the service principal can't obtain a token for the table

Check DATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRET, and the grants in Step 1.

No Connected to broker line, or the log shows a connection error

The hostname or port is wrong, MQTT isn't enabled for the workspace, or outbound TLS to port 8883 is blocked

Check ZEROBUS_MQTT_HOST against Step 2, and confirm the requirements in Requirements.

Rows don't appear in the table

Zerobus Ingest writes rows asynchronously, or the table name in DATABRICKS_TABLE_NAME doesn't match the table

Wait a minute or two, then check the table name and the grants.

The log shows disconnected client: 131 (0x83, Implementation specific error), and the connection drops

A record doesn't match the table, and the table has no column with the zerobus-rescue tag

Add the rescue column as Step 1 shows, and wait 5 minutes after you apply the tag. See Step 5.

The log shows Publish error: Payload format invalid

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.

Symptom

Cause

Fix

Token request failed with HTTP 400; nothing connected (or 401) in the log

The client ID or secret is wrong, or the service principal can't obtain a token for the table

Check DATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRET, and the grants in Step 1.

No Connected to broker line, or the log shows a connection error

The hostname or port is wrong, MQTT isn't enabled for the workspace, or outbound TLS to port 8883 is blocked

Check ZEROBUS_MQTT_HOST against Step 2, and confirm the requirements in Requirements.

Rows don't appear in the table

Zerobus Ingest writes rows asynchronously, or the table name in DATABRICKS_TABLE_NAME doesn't match the table

Wait a minute or two, then check the table name and the grants.

The log shows disconnected client: 131 (0x83, Implementation specific error), and the connection drops

A record doesn't match the table, and the table has no column with the zerobus-rescue tag

Add the rescue column as Step 1 shows, and wait 5 minutes after you apply the tag. See Step 5.

The log shows Publish error: Payload format invalid

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​