# Queue / Topic Consumer

> Consumes messages from an external Message Queue or Pub/Sub Topic.

Source: https://docs.sommatic.ai/en/nodes/queue.trigger/

- Slug: `queue.trigger`
- Family: Triggers
- Version: 1.0.0
- Status: Experimental. No queue consumer exists yet, so the trigger never starts a run by itself; `source`, `prefetch` and `ack_mode` are not read and nothing is acknowledged.
- Execution: async

Consumes messages from an external Message Queue or Pub/Sub Topic.

This trigger connects to external brokers (like RabbitMQ, Kafka, SQS, or Redis) and initiates a workflow instance for each incoming message.

**Key Features:**
- **Backpressure:** Supports `prefetch` to limit concurrent workflow instantiations.
- **Reliability:** Supports Auto or Manual Acknowledgement (Ack/Nack) to ensure message delivery guarantees.

**Use Cases:**
- Processing tasks offloaded from a main web server.
- Event-driven architecture integrating with legacy systems.

## Configuration

- `source` (string, required): Resource ID of the message broker connection.
- `subscription` (string, required): The specific channel to listen to.
- `prefetch` (integer, optional): Max unacknowledged messages. Default: `10`.
- `ack_mode` (string, optional): Auto: Ack on workflow start. Manual: Requires specific app to Ack. Default: `auto`. Allowed values: `auto`, `manual`.

Its configuration shows the shared execution settings, but this node does not apply retries (`retryOnFail`, `maxTries`) yet. `continueOnFail` is not applied by any node.

## Output ports

- `out` (default): Message Received
- `error`: Error

## Output contract

- `message_id` (string, optional)
- `queue` (string, optional)
- `content` (object, optional): The message body.
- `attributes` (object, optional): Message headers/metadata.

## In expressions

What a later node can read from this node's output with `$("Queue / Topic Consumer").item.json.<path>` (write your node's label instead of `Queue / Topic Consumer`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("Queue / Topic Consumer").item.json.message_id` (string) = `"msg-7f3a9c"`
- `$("Queue / Topic Consumer").item.json.queue` (string) = `"orders.created"`
- `$("Queue / Topic Consumer").item.json.content` (object) = `{…}`: The message body.
- `$("Queue / Topic Consumer").item.json.attributes` (object) = `{…}`: Message headers/metadata.
- `$("Queue / Topic Consumer").item.json.content.order_id` (string) = `"ord_123"` (Not suggested by the editor)
- `$("Queue / Topic Consumer").item.json.content.total` (number) = `129.99` (Not suggested by the editor)
- `$("Queue / Topic Consumer").item.json.attributes.content-type` (string) = `"application/json"` (Not suggested by the editor)


## How it works

Entry node for queue-driven workflows. It reshapes the run's input into `{ message_id, queue, content, attributes }` and rejects messages from another queue than `subscription`; in this version no broker consumer starts it.

1. What starts it: no backend component consumes a broker for this node in this version. The workflow runs when it is started explicitly with a message-shaped input `{ message_id, queue, content, attributes }`.
2. Resolves the node config. When `subscription` is set and the input's `queue` is set and different, it returns `error`.
3. Otherwise outputs `message_id` (default `unknown`), `queue` (the input's, or `subscription`), `content` (the input's `content`, or the whole input) and `attributes` (default `{}`).

## In a workflow

### Node

```json
{
  "node_id": "node-orders-queue",
  "label": "Orders queue",
  "operator_slug": "queue.trigger",
  "operator_version": "1.0.0",
  "config": {
    "source": "pi-credential-xxxx",
    "subscription": "orders.created",
    "prefetch": 10,
    "ack_mode": "auto"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "message_id": "msg-7f3a9c",
    "queue": "orders.created",
    "content": {
      "order_id": "ord_123",
      "total": 129.99
    },
    "attributes": {
      "content-type": "application/json"
    }
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The message is accepted.

```json
{
  "message_id": "msg-7f3a9c",
  "queue": "orders.created",
  "content": {
    "order_id": "ord_123",
    "total": 129.99
  },
  "attributes": {
    "content-type": "application/json"
  }
}
```

### `error`

The input's `queue` differs from `subscription`. The output is the envelope `{ ok, errors, port }`.

```json
{
  "ok": false,
  "errors": [
    "Queue mismatch: orders.created is not equal to orders.cancelled"
  ],
  "port": "error"
}
```

## Behaviour in the code

- `source`: Declared and required, but not read. `prefetch` and `ack_mode` are not read either: no message is acknowledged by this node.
- `subscription`: Only used to reject messages from another queue and as the default `queue` in the output.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `Queue mismatch: <subscription> is not equal to <queue>` | `error` | The workflow was started with a message from another queue. |

## Notes and limits

- When the input has no `content`, the whole input (including any `$trigger_state` the backend adds) becomes `content`.
- With sample data the engine skips the node and passes the sample input on `out` unchanged.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "queue.trigger",
  "name": "Queue / Topic Consumer",
  "category": "triggers",
  "version": "1.0.0",
  "description": "Consumes messages from an external Message Queue or Pub/Sub Topic.\n\n  This trigger connects to external brokers (like RabbitMQ, Kafka, SQS, or Redis) and initiates a workflow instance for each incoming message.\n\n  **Key Features:**\n  - **Backpressure:** Supports `prefetch` to limit concurrent workflow instantiations.\n  - **Reliability:** Supports Auto or Manual Acknowledgement (Ack/Nack) to ensure message delivery guarantees.\n\n  **Use Cases:**\n  - Processing tasks offloaded from a main web server.\n  - Event-driven architecture integrating with legacy systems.",
  "capabilities": [
    "queue-consumer",
    "high-throughput",
    "async-processing"
  ],
  "config_schema": {
    "type": "object",
    "required": [
      "source",
      "subscription"
    ],
    "properties": {
      "source": {
        "type": "string",
        "title": "Connection / Broker",
        "description": "Resource ID of the message broker connection."
      },
      "subscription": {
        "type": "string",
        "title": "Queue Name / Topic",
        "description": "The specific channel to listen to."
      },
      "prefetch": {
        "type": "integer",
        "default": 10,
        "title": "Prefetch Count",
        "description": "Max unacknowledged messages."
      },
      "ack_mode": {
        "type": "string",
        "enum": [
          "auto",
          "manual"
        ],
        "default": "auto",
        "title": "Ack Mode",
        "description": "Auto: Ack on workflow start. Manual: Requires specific app to Ack."
      },
      "execution": {
        "type": "object",
        "title": "Execution Settings",
        "properties": {
          "alwaysOutputData": {
            "type": "boolean",
            "title": "Always Output Data",
            "description": "Always output data, even on error (if configured).",
            "default": false
          },
          "retryOnFail": {
            "type": "boolean",
            "title": "Retry On Fail",
            "description": "Retry the execution if it fails.",
            "default": false
          },
          "maxTries": {
            "type": "integer",
            "title": "Max Retries",
            "description": "Number of retries.",
            "default": 3,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "waitBetweenTries": {
            "type": "integer",
            "title": "Wait Between Retries (ms)",
            "default": 1000,
            "displayOptions": {
              "show": {
                "retryOnFail": [
                  true
                ]
              }
            }
          },
          "continueOnFail": {
            "type": "boolean",
            "title": "Continue On Fail",
            "description": "Do not crash workflow on error.",
            "default": false
          },
          "timeout_ms": {
            "type": "integer",
            "title": "Timeout (ms)",
            "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
            "default": 300000
          }
        }
      }
    }
  },
  "input_contract_schema": null,
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "message_id": {
        "type": "string"
      },
      "queue": {
        "type": "string"
      },
      "content": {
        "type": "object",
        "description": "The message body."
      },
      "attributes": {
        "type": "object",
        "description": "Message headers/metadata."
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Message Received",
      "is_default": true
    },
    {
      "port_id": "error",
      "label": "Error"
    }
  ],
  "execution_mode": "async",
  "tags": [
    "queue",
    "consumer",
    "kafka",
    "rabbitmq"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_sommatic_ai",
    "profile": {
      "name": "Sommatic AI",
      "verified": true,
      "url": "https://me.veripass.com/sommatic",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "required": [
    "source",
    "subscription"
  ],
  "properties": {
    "source": {
      "type": "string",
      "title": "Connection / Broker",
      "description": "Resource ID of the message broker connection."
    },
    "subscription": {
      "type": "string",
      "title": "Queue Name / Topic",
      "description": "The specific channel to listen to."
    },
    "prefetch": {
      "type": "integer",
      "default": 10,
      "title": "Prefetch Count",
      "description": "Max unacknowledged messages."
    },
    "ack_mode": {
      "type": "string",
      "enum": [
        "auto",
        "manual"
      ],
      "default": "auto",
      "title": "Ack Mode",
      "description": "Auto: Ack on workflow start. Manual: Requires specific app to Ack."
    },
    "execution": {
      "type": "object",
      "title": "Execution Settings",
      "properties": {
        "alwaysOutputData": {
          "type": "boolean",
          "title": "Always Output Data",
          "description": "Always output data, even on error (if configured).",
          "default": false
        },
        "retryOnFail": {
          "type": "boolean",
          "title": "Retry On Fail",
          "description": "Retry the execution if it fails.",
          "default": false
        },
        "maxTries": {
          "type": "integer",
          "title": "Max Retries",
          "description": "Number of retries.",
          "default": 3,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "waitBetweenTries": {
          "type": "integer",
          "title": "Wait Between Retries (ms)",
          "default": 1000,
          "displayOptions": {
            "show": {
              "retryOnFail": [
                true
              ]
            }
          }
        },
        "continueOnFail": {
          "type": "boolean",
          "title": "Continue On Fail",
          "description": "Do not crash workflow on error.",
          "default": false
        },
        "timeout_ms": {
          "type": "integer",
          "title": "Timeout (ms)",
          "description": "Max execution time for this node before it is killed, in milliseconds. Honored by long-running operators (e.g. OCR/extraction, inline code). Default 300000 (5 min).",
          "default": 300000
        }
      }
    }
  }
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "message_id": {
      "type": "string"
    },
    "queue": {
      "type": "string"
    },
    "content": {
      "type": "object",
      "description": "The message body."
    },
    "attributes": {
      "type": "object",
      "description": "Message headers/metadata."
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Message Received",
    "is_default": true
  },
  {
    "port_id": "error",
    "label": "Error"
  }
]
```
