|
| 1 | +--- |
| 2 | +title: Input Streams hooks |
| 3 | +sidebarTitle: Input Streams |
| 4 | +description: Send data into running tasks from your React components |
| 5 | +--- |
| 6 | + |
| 7 | +These patterns allow you to send data into running tasks from your React frontend. This is useful for cancelling AI streams, approving workflows, or sending user input to interactive agents. |
| 8 | + |
| 9 | +<Note> |
| 10 | + To learn how to define and receive input stream data inside your tasks, see our [Input Streams](/tasks/input-streams) documentation. For backend usage, see the [backend input streams documentation](/realtime/backend/input-streams). |
| 11 | +</Note> |
| 12 | + |
| 13 | +## Sending data from React |
| 14 | + |
| 15 | +Input streams don't have a dedicated React hook — instead, you call `.send()` directly from your component event handlers, typically through an API route for security. |
| 16 | + |
| 17 | +### Via an API route (Recommended) |
| 18 | + |
| 19 | +The recommended approach is to send input stream data through your backend API routes, which keeps your secret API key server-side: |
| 20 | + |
| 21 | +```tsx |
| 22 | +"use client"; |
| 23 | + |
| 24 | +import { useRealtimeStream } from "@trigger.dev/react-hooks"; |
| 25 | +import { aiOutput } from "@/trigger/streams"; |
| 26 | + |
| 27 | +export function AIChat({ |
| 28 | + runId, |
| 29 | + accessToken, |
| 30 | +}: { |
| 31 | + runId: string; |
| 32 | + accessToken: string; |
| 33 | +}) { |
| 34 | + const { parts, error } = useRealtimeStream(aiOutput, runId, { |
| 35 | + accessToken, |
| 36 | + timeoutInSeconds: 300, |
| 37 | + }); |
| 38 | + |
| 39 | + const handleCancel = async () => { |
| 40 | + await fetch("/api/cancel", { |
| 41 | + method: "POST", |
| 42 | + headers: { "Content-Type": "application/json" }, |
| 43 | + body: JSON.stringify({ runId }), |
| 44 | + }); |
| 45 | + }; |
| 46 | + |
| 47 | + if (error) return <div>Error: {error.message}</div>; |
| 48 | + if (!parts) return <div>Loading...</div>; |
| 49 | + |
| 50 | + return ( |
| 51 | + <div> |
| 52 | + <div>{parts.join("")}</div> |
| 53 | + <button onClick={handleCancel}>Stop generating</button> |
| 54 | + </div> |
| 55 | + ); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +With the corresponding API route: |
| 60 | + |
| 61 | +```ts app/api/cancel/route.ts |
| 62 | +import { cancelStream } from "@/trigger/streams"; |
| 63 | + |
| 64 | +export async function POST(req: Request) { |
| 65 | + const { runId } = await req.json(); |
| 66 | + await cancelStream.send(runId, { reason: "User clicked stop" }); |
| 67 | + return Response.json({ cancelled: true }); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Approval workflow UI |
| 72 | + |
| 73 | +Here's a complete example of a human-in-the-loop approval interface: |
| 74 | + |
| 75 | +```tsx |
| 76 | +"use client"; |
| 77 | + |
| 78 | +import { useRealtimeRun } from "@trigger.dev/react-hooks"; |
| 79 | +import type { draftEmailTask } from "@/trigger/draft-email"; |
| 80 | + |
| 81 | +export function ApprovalUI({ |
| 82 | + runId, |
| 83 | + accessToken, |
| 84 | +}: { |
| 85 | + runId: string; |
| 86 | + accessToken: string; |
| 87 | +}) { |
| 88 | + const { run, error } = useRealtimeRun<typeof draftEmailTask>(runId, { |
| 89 | + accessToken, |
| 90 | + }); |
| 91 | + |
| 92 | + const handleApprove = async (approved: boolean) => { |
| 93 | + await fetch("/api/approve", { |
| 94 | + method: "POST", |
| 95 | + headers: { "Content-Type": "application/json" }, |
| 96 | + body: JSON.stringify({ |
| 97 | + runId, |
| 98 | + approved, |
| 99 | + reviewer: "current-user@example.com", |
| 100 | + }), |
| 101 | + }); |
| 102 | + }; |
| 103 | + |
| 104 | + if (error) return <div>Error: {error.message}</div>; |
| 105 | + if (!run) return <div>Loading...</div>; |
| 106 | + |
| 107 | + return ( |
| 108 | + <div> |
| 109 | + <div>Status: {run.status}</div> |
| 110 | + {run.status === "EXECUTING" && ( |
| 111 | + <div> |
| 112 | + <p>Draft is ready for review. Please approve or reject:</p> |
| 113 | + <button onClick={() => handleApprove(true)}>Approve</button> |
| 114 | + <button onClick={() => handleApprove(false)}>Reject</button> |
| 115 | + </div> |
| 116 | + )} |
| 117 | + {run.status === "COMPLETED" && run.output && ( |
| 118 | + <div> |
| 119 | + Result: {run.output.sent ? "Email sent!" : "Email rejected"} |
| 120 | + </div> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + ); |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### Combining with output streams |
| 128 | + |
| 129 | +A common pattern is to read output streams while sending input streams — for example, displaying an AI response while allowing the user to cancel: |
| 130 | + |
| 131 | +```tsx |
| 132 | +"use client"; |
| 133 | + |
| 134 | +import { useRealtimeStream, useRealtimeRun } from "@trigger.dev/react-hooks"; |
| 135 | +import { aiOutput } from "@/trigger/streams"; |
| 136 | +import type { aiTask } from "@/trigger/ai-task"; |
| 137 | + |
| 138 | +export function InteractiveAI({ |
| 139 | + runId, |
| 140 | + accessToken, |
| 141 | +}: { |
| 142 | + runId: string; |
| 143 | + accessToken: string; |
| 144 | +}) { |
| 145 | + const { run } = useRealtimeRun<typeof aiTask>(runId, { accessToken }); |
| 146 | + const { parts } = useRealtimeStream(aiOutput, runId, { |
| 147 | + accessToken, |
| 148 | + timeoutInSeconds: 300, |
| 149 | + }); |
| 150 | + |
| 151 | + const handleCancel = async () => { |
| 152 | + await fetch("/api/cancel", { |
| 153 | + method: "POST", |
| 154 | + headers: { "Content-Type": "application/json" }, |
| 155 | + body: JSON.stringify({ runId }), |
| 156 | + }); |
| 157 | + }; |
| 158 | + |
| 159 | + const isRunning = run?.status === "EXECUTING"; |
| 160 | + |
| 161 | + return ( |
| 162 | + <div> |
| 163 | + <div>{parts?.join("") ?? "Waiting for response..."}</div> |
| 164 | + {isRunning && ( |
| 165 | + <button onClick={handleCancel}>Stop generating</button> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + ); |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +## Important notes |
| 173 | + |
| 174 | +- Always send input stream data through your backend API routes to keep your API keys secure |
| 175 | +- Input streams require [Realtime Streams v2](/tasks/streams#enabling-streams-v2) (enabled by default in SDK 4.1.0+) |
| 176 | +- You cannot send data to a completed, failed, or canceled run |
| 177 | +- For the complete task-side API (`once()`, `on()`, `peek()`), see the [Input Streams guide](/tasks/input-streams) |
0 commit comments