Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

queueing.md

92 lines3.5 KB

1---
2title: Message Queue
3id: message-queue
4order: 4
5description: "Control what happens when the user sends while a reply is still streaming: queue, drop, or interrupt."
6keywords:
7 - tanstack ai
8 - message queue
9 - queue
10 - whenBusy
11 - interrupt
12 - sendMessage
13 - cancelQueued
14---
15​
16The user hits send while the reply is in flight. You need a rule for that message: wait, ignore, or cut in.
17​
18By default the message waits in `queue`. It sends after the current run succeeds.
19​
20## 1. Show the pending queue
21​
22`useChat` exposes `queue` separately from `messages`. `cancelQueued(id)` drops an item before it sends:
23​
24```tsx group=queueing-messages
25import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
26​
27const { messages, queue, sendMessage, cancelQueued } = useChat({
28 connection: fetchServerSentEvents("/api/chat"),
29});
30```
31​
32```tsx group=queueing-messages
33function PendingQueue() {
34 return (
35 <>
36 {queue.map((item) => (
37 <div key={item.id} className="pending">
38 {typeof item.content === "string" ? item.content : "[attachment]"}
39 <button onClick={() => cancelQueued(item.id)}>Cancel</button>
40 </div>
41 ))}
42 </>
43 );
45```
46​
47Render `queue` with a different style from `messages`. Once the text appears in `queue` or `messages`, clear the composer.
48​
49## 2. Pick `whenBusy`
50​
51Pass a `queue` option. A string is shorthand for `{ whenBusy }`:
52​
53```tsx group=queueing-whenbusy
54import { useChat, fetchServerSentEvents } from "@tanstack/ai-react";
55​
56const { sendMessage } = useChat({
57 connection: fetchServerSentEvents("/api/chat"),
58 queue: { whenBusy: "queue", drain: "fifo", maxSize: 5 },
59});
60```
61​
62`whenBusy` is what happens to a send that arrives while the client is busy. The client is busy when a stream is active, a send is in flight, or the queue is draining:
63​
64- `"queue"` (default): hold the message. It sends once the run settles successfully.
65- `"drop"`: ignore the send. The promise still resolves. The message never appears in `queue` or `messages`. Keep the composer text and show feedback if you want a retry.
66- `"interrupt"`: abort the current stream and send the new message now. Already-queued messages stay. They drain after the interrupting send succeeds.
67​
68Override the policy for one send with the second argument to `sendMessage`:
69​
70```tsx group=queueing-whenbusy
71sendMessage("Never mind, do this instead", {
72 whenBusy: "interrupt",
73 body: { source: "composer" },
74});
75```
76​
77`body` is extra JSON for that request only. It is merged into `forwardedProps`.
78​
79## Later
80​
81- **`drain`**: `"fifo"` (default) sends queued items one at a time in order. `"batch"` merges the queue into one send after a successful settle (strings join with `\n`, multimodal parts stay in order, last `body` wins).
82- **`maxSize`**: caps how many messages can wait (`0` means never queue).
83- **`onOverflow`**: `"reject"` (default) ignores a send once `maxSize` is reached. `"drop-oldest"` evicts the oldest queued item to make room.
84- **Strategy function**: pass a function as `queue` for per-send control. That form always drains FIFO. Per-call `whenBusy` still wins.
85​
86### When the queue drains versus flushes
87​
88- **Drain (auto-send)**: only after a successful stream settle, including after tool continuations finish.
89- **Flush (discard without sending)**: on error or abort of the active generation (`stop()`, real stream errors), `clear()`, `unsubscribe()`, and `reload()`.
90- **`interrupt` does not flush**: existing queued items remain. They drain after the interrupting turn succeeds.
91​
92The composer no longer fights the stream. A send while busy waits, drops, or cuts in, on purpose.