# Queue

Send slow work to a queue so your route replies at once, and the work runs later.

Hosts: Node, Docker, Cloudflare, Vercel.

:hero-figure{name="queue"}

```ts [server/api/signup.post.ts]
import { defineEventHandler, readValidatedBody } from 'h3'
import * as v from 'valibot'
import { sendQueue } from 'vite-hub/queue'

// Returns when the queue accepts the job, before it runs
export default defineEventHandler(async (event) => {
  const { email } = await readValidatedBody(event, body => v.parse(v.object({ email: v.string() }), body))
  const [error, result] = await sendQueue('welcome-email', { email })
  if (error) throw error
  return result
})
```

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

// The file name is the queue name.
// This runs later, after the route replies.
export default defineQueue<{ email: string }>(
  async ({ payload, attempts }) => {
    // Send the email here
    console.log(payload.email, attempts)
  },
)
```

```ts [server/api/reminder.post.ts]
import { defineEventHandler, readValidatedBody } from 'h3'
import * as v from 'valibot'
import { sendQueue } from 'vite-hub/queue'

// Run the same job in 10 minutes
export default defineEventHandler(async (event) => {
  const { email } = await readValidatedBody(event, body => v.parse(v.object({ email: v.string() }), body))
  const [error, result] = await sendQueue('welcome-email', { email }, { delaySeconds: 600 })
  if (error) throw error
  return result
})
```

:::framework{name="Nuxt"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    host: 'cloudflare',
    // On Cloudflare, jobs go to Cloudflare Queues
    queue: true,
  },
})
```
:::
:::framework{name="Nitro"}
```ts [vite.config.ts]
import { nitro } from 'nitro/vite'
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      host: 'cloudflare',
      // On Cloudflare, jobs go to Cloudflare Queues
      queue: true,
    }),
    nitro(),
  ],
})
```
:::
:::framework{name="Vite"}
```ts [vite.config.ts]
import { defineConfig } from 'vite'
import { vitehub } from 'vite-hub'

export default defineConfig({
  plugins: [
    vitehub({
      host: 'cloudflare',
      // On Cloudflare, jobs go to Cloudflare Queues
      queue: true,
    }),
  ],
})
```
:::

### [One file per queue](https://vitehub.dev/docs/queue/configure#define-a-queue)

Add a file to `server/queues`. The file name is the queue name, and its function runs each job.

### [Reply first, work later](https://vitehub.dev/docs/queue/server-api#enqueue-work)

`sendQueue()` returns when the queue accepts the job. The job runs after your route replies.

### [Wait before a job runs](https://vitehub.dev/docs/queue/server-api#queue-enqueue-options)

Set `delaySeconds` to start a job later.

### [Typed names and data](https://vitehub.dev/docs/queue/server-api#definition-owned-dispatch-types)

A wrong queue name or wrong job data is a type error in your editor.

### [Failed jobs run again](https://vitehub.dev/docs/queue/server-api#handle-failures)

A failed job can run again, so make a second run safe.

### [Node, Cloudflare, or Vercel](https://vitehub.dev/docs/queue/configure#drivers)

On Node, jobs run in the server process, on one instance only. On Cloudflare and Vercel, they run on Cloudflare Queues or Vercel Queues.

Use Queue to send work to a consumer that can run after the request ends. Queue Definitions own consumer code, retries, and concurrency.

Treat enqueue as a handoff. Make the job idempotent, keep payloads bounded, and choose retry behavior for the side effect. Queue configuration does not make a job exactly once.

Use a Queue for independent background work, a Schedule for when work starts,
and a Workflow when several durable steps must be inspected together.

Prove enqueue, consumer execution, retry, and a poison or invalid payload. Inspect the Nitro output for the target host.

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

- [Overview](https://vitehub.dev/raw/docs/queue.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/queue/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/queue/configure.md)
- [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_PROVIDER_FAILED](https://vitehub.dev/raw/docs/queue/diagnostics/queue_provider_failed.md)

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