Integrations

Vercel AI SDK

useChat answers that survive a refresh, a dropped connection and a second tab, with Stop from any of them.

PulseChatTransport is a transport for the AI SDK's useChat. Your route starts the answer and returns at once, and the answer streams through a Pulse channel instead of the HTTP response. A refresh, a dropped connection or another tab picks it up where it is, and Stop works from any of them.

Terminal
bash
npm install @anishroy/pulse ai @ai-sdk/react

The route

Keep your streamText call. Write its UI message stream to the chat's channel with pipe, and answer the request with a 202.

app/api/chat/route.ts
typescript
import { Pulse } from "@anishroy/pulse/server"import { convertToModelMessages, streamText, type UIMessage } from "ai"import { after } from "next/server"
const rt = new Pulse()
export async function POST(req: Request) { const { id, messages }: { id: string; messages: UIMessage[] } = await req.json() const s = rt.stream(`chat:${id}`) const result = streamText({ model, messages: await convertToModelMessages(messages), abortSignal: s.signal, // Stop, from any tab }) // The answer outlives this request after(() => s.pipe( result.toUIMessageStream({ originalMessages: messages, onFinish: ({ messages }) => saveChat(id, messages), }) ) ) return new Response(null, { status: 202 })}
  • after keeps the function running until the answer ends. Elsewhere, use your platform's waitUntil; on a long-running server, don't await pipe.
  • Save messages in onFinish as usual and pass them to useChat as messages. The channel holds the answer while it streams, not your chat history.
  • Everything useChat streams passes through as is: text, reasoning, tool calls, approvals and data parts.

The chat

Give useChat the transport. It sends the same request as the default transport (id, messages, trigger, messageId and your body), then reads the answer from the channel.

chat.tsx
tsx
"use client"import { useChat } from "@ai-sdk/react"import { PulseChatTransport } from "@anishroy/pulse/ai-sdk"import { createClient } from "@anishroy/pulse/client"import type { UIMessage } from "ai"
const pulse = createClient({ url: PULSE_URL, auth: "/api/pulse/token" })const transport = new PulseChatTransport({ client: pulse })
export function Chat({ id, saved }: { id: string; saved: UIMessage[] }) { const { messages, sendMessage, status } = useChat({ id, messages: saved, transport, resume: true, // pick up an answer that's still streaming })
return ( <> {messages.map((m) => <Message key={m.id} message={m} />)} {status === "streaming" && ( <button onClick={() => transport.stop(id)}>Stop</button> )} <Composer onSend={(text) => sendMessage({ text })} /> </> )}

With PulseProvider, pass its client (usePulseClient()) as client instead, in a useMemo, to share its connections.

Stop

transport.stop(id) stops the answer for everyone: the route's s.signal aborts, which cancels the model call, and every tab's stream ends as aborted. useChat's own stop() only stops this tab listening, and useChat also calls it when the component unmounts, so leaving the page never stops an answer.

The token

The browser's token must allow the chat's channel, and publishing to it for Stop. Scope it to the user:

app/api/pulse/token/route.ts
typescript
export const POST = createTokenRoute({  authorize: async () => {    const user = await currentUser()    if (!user) return null    const chats = `chat:${user.id}:*`    return { user: user.id, channels: [chats], publish: [chats] }  },})
// Then name each chat's channel the same way on both sides:// new PulseChatTransport({ client, channel: (id) => `chat:${userId}:${id}` })// rt.stream(`chat:${user.id}:${id}`)