Queue (Ordered Delivery)

Per-key async task queue with configurable concurrency and backpressure. With the default concurrency: 1, tasks are processed strictly in order per key - useful for sequential operations like collaborative editing, turn-based games, or transaction sequences. With concurrency > 1, dequeue order is preserved but tasks run in parallel, so completion order is not guaranteed.

Setup

// src/lib/server/queue.js
import { createQueue } from 'svelte-adapter-uws/plugins/queue';

// Sequential processing per key (default concurrency: 1)
export const queue = createQueue({ maxSize: 100 });

Usage

// src/hooks.ws.js
import { queue } from '$lib/server/queue';

export async function message(ws, { data, platform }) {
  const msg = JSON.parse(Buffer.from(data).toString());

  // Messages for the same topic are processed one at a time
  const result = await queue.push(msg.topic, async () => {
    const record = await db.update(msg.data);
    platform.publish(msg.topic, 'updated', record);
    return record;
  });
}

Different keys are independent - push('room-a', ...) and push('room-b', ...) run concurrently. Only tasks with the same key are queued.

Options

OptionDefaultDescription
concurrency1Max concurrent tasks per key
maxSizeInfinityMax waiting tasks per key (rejects when exceeded)
onDropnullCalled with { key, task } when a task is rejected

API

MethodDescription
queue.push(key, task)Enqueue a task, returns promise with the task’s return value
queue.size(key?)Waiting + running count for a key, or total
queue.clear(key?)Cancel waiting tasks (running tasks continue)
queue.drain(key?)Wait for all tasks to complete

Limitations

  • Server-side only. No client component.
  • In-memory. Queue state lives in the process. Not durable across restarts.
  • No cancellation. Running tasks cannot be aborted. clear() only rejects waiting tasks.

Was this page helpful?