---
title: Send data to Zerobus Ingest from Node-RED
description: Publish JSON records from Node-RED flows to Unity Catalog Delta tables through the Zerobus Ingest MQTT 5 endpoint, with a rescue column and OAuth token refresh.
canonical_url: https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red
---

# Send data to Zerobus Ingest from Node-RED

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md). To use it, a workspace admin must turn on ****Zerobus Ingest MQTT Endpoint**** from the **Previews** page. See [Manage Databricks previews](https://docs.databricks.com/aws/en/admin/workspace-settings/manage-previews.md).

> [!NOTE]
>
> [Node-RED](https://nodered.org/) 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](https://nodered.org/docs/user-guide/nodes).
>
> Maintained by: the Node-RED open-source project, in the [node-red/node-red](https://github.com/node-red/node-red) repository.

Node-RED publishes each message to the [Zerobus Ingest MQTT endpoint](https://docs.databricks.com/aws/en/ingestion/zerobus-mqtt.md), 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](https://docs.databricks.com/aws/en/ingestion/zerobus-rescue-column.md). Without it, one record that doesn't match the table closes the whole MQTT connection. See [Step 5](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#bad-records).

## <a id="when-to-use" />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.

## <a id="how-it-works" />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](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#reconnect).

## <a id="requirements" />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](https://docs.databricks.com/aws/en/ingestion/zerobus-mqtt.md#prerequisites).
* A workspace in a region that supports Zerobus Ingest. MQTT availability can differ from general Zerobus Ingest availability. See [Prerequisites](https://docs.databricks.com/aws/en/ingestion/zerobus-mqtt.md#prerequisites) and [Availability](https://docs.databricks.com/aws/en/ingestion/zerobus-quotas.md#availability).
* A service principal with an OAuth secret. See [Configure authentication](https://docs.databricks.com/aws/en/ingestion/zerobus-ingest.md#service-principal). Note the service principal's client ID and OAuth secret.
* A target table that meets the [Table requirements](https://docs.databricks.com/aws/en/ingestion/zerobus-concepts.md#table-requirements), with nullable columns and a `rescue VARIANT` column with the `zerobus-rescue` tag. The rescue column is required. [Step 1](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#create-table) 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 Link](https://docs.databricks.com/aws/en/ingestion/zerobus-networking.md#front-end-private-link).

### <a id="create-table" />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](https://docs.databricks.com/aws/en/ingestion/zerobus-ingest.md#service-principal).

### <a id="endpoints" />Get your endpoints

Find your workspace URL, workspace ID, and region. See [Get your workspace URL and Zerobus Ingest endpoint](https://docs.databricks.com/aws/en/ingestion/zerobus-ingest.md#get-your-workspace-url-and-zerobus-ingest-endpoint). The MQTT hostname uses this format, on port `8883`:

`<workspace-id>.zerobus.<region>.cloud.databricks.com`

### <a id="configure" />Start Node-RED with the example flow

The [Node-RED MQTT example](https://github.com/databricks-solutions/zerobus-ingest-examples/tree/main/example_clients/mqtt_node_red_zerobus) 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](https://nodered.org/docs/getting-started/docker).

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](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#create-table), `<catalog>.<schema>.<table>`. |
   | `ZEROBUS_MQTT_HOST`        | The MQTT hostname from [Step 2](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#endpoints), 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](https://nodered.org/docs/user-guide/runtime/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.

### <a id="reconnect" />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.

### <a id="bad-records" />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. |

Without the rescue column, the watch timer reconnects the flow after a bad record, but the records sent during that gap are lost.

## <a id="troubleshooting" />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](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#create-table).                                                                                                         |
| 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](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#endpoints), and confirm the requirements in [Requirements](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#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](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#create-table) shows, and wait 5 minutes after you apply the tag. See [Step 5](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#bad-records). |
| 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](https://docs.databricks.com/aws/en/ingestion/zerobus-integrations/node-red.md#reconnect).                                                                               |

## <a id="additional-resources" />Additional resources

* [Use MQTT with Zerobus Ingest](https://docs.databricks.com/aws/en/ingestion/zerobus-mqtt.md): the MQTT endpoint's connection model, authentication, limits, and a Python example.
* [Node-RED example flow](https://github.com/databricks-solutions/zerobus-ingest-examples/tree/main/example_clients/mqtt_node_red_zerobus): the flow, settings, configuration template, and tests.
* [Node-RED documentation](https://nodered.org/docs/): running Node-RED, the flow editor, and the Docker image.
