# Queue configuration

## Configure the Vite Integration

A file in `server/queues/` turns on Queue, so you do not add a `queue` option. Set `queue` to an object only to change the options below, or `queue: false` to keep Queue off. The `host` picks the Queue Provider (the service that stores and delivers jobs).

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: { host: 'cloudflare' },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({ host: 'cloudflare' }), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({ host: 'cloudflare' })],
})
```

With `vitehub()`, the host sets `driver`, and on Cloudflare the app name sets `namePrefix`. On a Node server, ViteHub picks `postgres` when the app has a Postgres Default Database, else `local`. To pick it yourself, set `queue: { driver: 'postgres' }` or `queue: { driver: 'local' }`:

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: { queue: { driver: 'postgres' } },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({ queue: { driver: 'postgres' } }), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({ queue: { driver: 'postgres' } })],
})
```

Set other Queue options directly in `vitehub({ queue: { ... } })` or the Nuxt `vitehub.queue` object. An option that you set wins over the host:

### Nuxt

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: { host: 'vercel', queue: { driver: 'vercel', region: 'iad1' } },
})
```

### Nitro

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({ host: 'vercel', queue: { driver: 'vercel', region: 'iad1' } }), nitro()],
})
```

### Vite

```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [vitehub({ host: 'vercel', queue: { driver: 'vercel', region: 'iad1' } })],
})
```

With `@vite-hub/queue` alone, pass the same options to `hubQueue()` from `@vite-hub/queue/vite`.

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `driver` | `'cloudflare' \| 'vercel' \| 'local' \| 'postgres'` | inferred | The Queue Provider. Cloudflare on Cloudflare hosting, and Vercel for other production builds. On a Node server, `postgres` with a Postgres Default Database, else `local`. On Netlify, set `driver` or turn Queue off. |
| `cache` | `boolean` | `true` | Reuses named QueueClients where the driver allows it. Cloudflare clients still read the request's binding on each request. |
| `binding` | `string` | derived | Cloudflare only. Overrides the generated binding name. Works with one Queue Definition only. |
| `namePrefix` | `string` | none | Cloudflare only. Prefix of generated queue names: `<namePrefix>queue--<hex-name>`. |
| `region` | `string` | resolved at runtime | Vercel only. Default Vercel Queue region. |
| `base` | `string` | `.vitehub/data/queue` | Local only. Folder of stored jobs, relative to the server's working directory. |

### `queue: false`

Turns Queue off. The server then has no Queue runtime, so `sendQueue()` throws `QUEUE_DEFINITION_NOT_FOUND`. This applies to `vitehub()` and to `hubQueue(false)` from `@vite-hub/queue`. `hubQueue(false)` also adds no Vercel consumer functions. In Vitest, the generated test setup installs the `false` config, so Queue calls throw `QUEUE_DISABLED`.

## Drivers

| Driver | Configure with | Generated output | Notes |
| --- | --- | --- | --- |
| Cloudflare | `queue: { driver: 'cloudflare' }` | Worker queue handler and `wrangler.json` `queues.producers` / `queues.consumers` entries. | Uses the request's queue bindings. Supports `contentType` and `delay`. |
| Vercel | `queue: { driver: 'vercel', region?: string }` | One Nitro route per Queue Definition with a Vercel queue trigger. | Needs `@vercel/queue` and `@vercel/functions` in the app. Supports idempotency, region, retention, and delay. |
| Local | `queue: { driver: 'local', base?: string }` | A Nitro plugin that runs stored jobs in the server process. | Default of the Node host without a Postgres Database. Supports `delay` and `idempotencyKey`. Jobs stay on one host. `pnpm dev` uses it for every driver, with that driver's rules. |
| Postgres | `queue: { driver: 'postgres' }` | A Nitro plugin that starts workers in each server process and stops them when Nitro closes. | Default of the Node host with a Postgres Default Database. Uses the Default Database's connection. `QUEUE_POSTGRES_URL` overrides it, and an explicit driver without a Postgres Database needs it. Every instance runs jobs. Supports `delay` and `idempotencyKey`. Install `postgres`. |

ViteHub has no Queue Provider for Deno. The local driver keeps jobs on one host, so read [Local delivery](https://vitehub.dev/docs/queue/limits-and-errors#local-delivery) before you run more than one server. For more than one Node server, use the postgres driver: see [Postgres delivery](https://vitehub.dev/docs/queue/limits-and-errors#postgres-delivery). Check the [support matrix](https://vitehub.dev/docs/frameworks-hosts/support-matrix) before you pick a host.

### Cloudflare names

Without `binding`, the binding comes from the queue name, such as `QUEUE_77656C636F6D65` for `welcome`. Cloudflare queue names are `<namePrefix>queue--<hex-name>`. A name over 63 characters becomes a readable prefix plus a digest. Do not depend on these names. Call `sendQueue()` with the queue name.

### Vercel region and topics

Without `region`, ViteHub reads `QUEUE_REGION`, then `VERCEL_REGION`, then the headers of a Vercel request.

Vercel topic names are `topic--<hex-name>`. Do not depend on them. Call `sendQueue()` with the queue name.

## Define a queue

Create a Queue Definition (the file that handles one queue's jobs) in `server/queues/<name>.ts` or `src/<name>.queue.ts`.

```ts [server/queues/welcome-email.ts] fragment
import { defineQueue } from 'vite-hub/queue'

export default defineQueue<{ email: string }>({
  handler: async ({ payload }) => {
    await sendWelcomeEmail(payload.email)
  },
})
```

The file path gives the queue name. Runtime helpers call this file `welcome-email`.

## Queue Definition options

Put the options next to `handler` in the object that you pass to `defineQueue()`.

```ts [server/queues/report.ts] fragment
import { defineQueue } from 'vite-hub/queue'

export default defineQueue<{ reportId: string }>({
  handler: async ({ payload }) => {
    await buildReport(payload.reportId)
  },
  concurrency: 5,
})
```

| Option | Type | Provider | Description |
| --- | --- | --- | --- |
| `cache` | `boolean` | All | Overrides QueueClient caching for this queue. |
| `concurrency` | `number` | Cloudflare, Local, Postgres | How many jobs of this queue run at the same time: in one batch on Cloudflare, in one server process on local and postgres. Default `1`. Rounded down to an integer, at least `1`. |
| `maxBatchSize` | `number` | Cloudflare | The most messages in one batch, from 1 to 100. Cloudflare's default is 10. Write it as a number in `export default defineQueue({ ... })`: the build reads it from the file and writes `max_batch_size` on the consumer. |
| `onError` | `(error, message, batch) => 'ack' \| 'retry' \| { retry: { delay?: Duration } } \| void` | Cloudflare | Handles delivery errors. See [Handle failures](https://vitehub.dev/docs/queue/server-api#handle-failures). |
| `callbackOptions` | `{ retry?: VercelQueueRetryHandler, visibilityTimeoutSeconds?: number }` | Vercel | Callback options for `@vercel/queue` for this queue. |
| `onDispatchError` | `(error, context) => unknown \| Promise<unknown>` | All | Handles dispatch errors from `deferQueue()`, not delivery errors. |

On Cloudflare, write Queue options in an object literal without spreads or computed keys. If you keep a Wrangler consumer for the same queue, its `max_batch_size` must match `maxBatchSize` when you set that option. The build rejects a missing or different value and leaves the consumer's other settings, such as `max_retries`, alone.

## Contribute Queues from a package

A package can add a queue from its Vite plugin, so apps do not write `server/queues/<name>.ts`. Add `vitehub.queues` to the plugin:

```ts [my-package/vite.ts] fragment
import { fileURLToPath } from 'node:url'
import type { Plugin } from 'vite'
import type { QueueContributingPlugin } from '@vite-hub/queue/vite'

export function myPackage(): Plugin & QueueContributingPlugin {
  return {
    name: 'my-package/vite',
    vitehub: {
      queues: [{
        name: 'my-package-deliveries',
        definition: { module: fileURLToPath(new URL('./deliver.js', import.meta.url)) },
        reason: 'Delivers webhooks after their delay.',
      }],
    },
  }
}
```

| Field | Type | Description |
| --- | --- | --- |
| `name` | `string` | Queue name for `sendQueue()` and provider output. Letters, numbers, `.`, `_`, `-`, and `/`. |
| `definition.module` | `string` | Absolute file path or `file:` URL of a module with `export default defineQueue(...)`. |
| `definition.export` | `string` | Must be `default`. Default `default`. |
| `reason` | `string` | Optional. Why the package adds it. Shown by `vitehub inspect definitions` and the Console. |

`vitehub.queues` accepts an array or a factory, which can be async and gets `{ root, projectRoot, serverDirs }`. A package queue works like a `server/queues/` file on every host: it gets the generated types, a Cloudflare binding and consumer, a Vercel consumer route, provisioning, and the local or postgres worker on Node.

Build errors:

- `QUEUE_DEFINITION_DUPLICATE`: the name is already used by a file or another plugin. The message names both.
- `QUEUE_CONTRIBUTION_INVALID`: the entry is invalid, or the module does not exist.

`collectQueueContributions(plugins, context)` from `@vite-hub/queue/vite` returns the checked list for other build steps.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/queue.md)
- [Get started](https://vitehub.dev/raw/docs/queue/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/queue/configure.md) (this page)
- [Server API](https://vitehub.dev/raw/docs/queue/server-api.md)
- [Hosts](https://vitehub.dev/raw/docs/queue/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/queue/limits-and-errors.md)
- [Concepts](https://vitehub.dev/raw/docs/queue/concepts.md)

Next: [QUEUE_UNSUPPORTED_OPTION](https://vitehub.dev/raw/docs/queue/diagnostics/queue_unsupported_option.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
