|
| 1 | +<<<<<<< ours |
1 | 2 | --- |
2 | 3 | title: Input Streams |
3 | 4 | sidebarTitle: Input Streams |
@@ -149,3 +150,153 @@ try { |
149 | 150 | - You cannot send data to a completed, failed, or canceled run |
150 | 151 | - Data sent before a listener is registered inside the task is **buffered** and delivered when a listener attaches |
151 | 152 | - Input streams require [Realtime Streams v2](/tasks/streams#enabling-streams-v2) (enabled by default in SDK 4.1.0+) |
| 153 | +||||||| |
| 154 | +======= |
| 155 | +--- |
| 156 | +title: Input Streams |
| 157 | +sidebarTitle: Input Streams |
| 158 | +description: Send data into running tasks from your backend code |
| 159 | +--- |
| 160 | + |
| 161 | +The Input Streams API allows you to send data into running Trigger.dev tasks from your backend code. This enables bidirectional communication — while [output streams](/realtime/backend/streams) let you read data from tasks, input streams let you push data into them. |
| 162 | + |
| 163 | +<Note> |
| 164 | + To learn how to receive input stream data inside your tasks, see our [Input Streams](/tasks/input-streams) documentation. For frontend applications using React, see our [React hooks input streams documentation](/realtime/react-hooks/input-streams). |
| 165 | +</Note> |
| 166 | + |
| 167 | +## Sending data to a running task |
| 168 | + |
| 169 | +### Using defined input streams (Recommended) |
| 170 | + |
| 171 | +The recommended approach is to use [defined input streams](/tasks/input-streams#defining-input-streams) for full type safety: |
| 172 | + |
| 173 | +```ts |
| 174 | +import { cancelSignal, approval } from "./trigger/streams"; |
| 175 | + |
| 176 | +// Cancel a running AI stream |
| 177 | +await cancelSignal.send(runId, { reason: "User clicked stop" }); |
| 178 | + |
| 179 | +// Approve a draft |
| 180 | +await approval.send(runId, { approved: true, reviewer: "alice@example.com" }); |
| 181 | +``` |
| 182 | + |
| 183 | +The `.send()` method is fully typed — the data parameter must match the generic type you defined on the input stream. |
| 184 | + |
| 185 | +## Practical examples |
| 186 | + |
| 187 | +### Cancel from a Next.js API route |
| 188 | + |
| 189 | +```ts app/api/cancel/route.ts |
| 190 | +import { cancelStream } from "@/trigger/streams"; |
| 191 | + |
| 192 | +export async function POST(req: Request) { |
| 193 | + const { runId } = await req.json(); |
| 194 | + |
| 195 | + await cancelStream.send(runId, { reason: "User clicked stop" }); |
| 196 | + |
| 197 | + return Response.json({ cancelled: true }); |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +### Approval workflow API |
| 202 | + |
| 203 | +```ts app/api/approve/route.ts |
| 204 | +import { approval } from "@/trigger/streams"; |
| 205 | + |
| 206 | +export async function POST(req: Request) { |
| 207 | + const { runId, approved, reviewer } = await req.json(); |
| 208 | + |
| 209 | + await approval.send(runId, { |
| 210 | + approved, |
| 211 | + reviewer, |
| 212 | + }); |
| 213 | + |
| 214 | + return Response.json({ success: true }); |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +### Remix action handler |
| 219 | + |
| 220 | +```ts app/routes/api.approve.ts |
| 221 | +import { json, type ActionFunctionArgs } from "@remix-run/node"; |
| 222 | +import { approval } from "~/trigger/streams"; |
| 223 | + |
| 224 | +export async function action({ request }: ActionFunctionArgs) { |
| 225 | + const formData = await request.formData(); |
| 226 | + const runId = formData.get("runId") as string; |
| 227 | + const approved = formData.get("approved") === "true"; |
| 228 | + const reviewer = formData.get("reviewer") as string; |
| 229 | + |
| 230 | + await approval.send(runId, { approved, reviewer }); |
| 231 | + |
| 232 | + return json({ success: true }); |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +### Express handler |
| 237 | + |
| 238 | +```ts |
| 239 | +import express from "express"; |
| 240 | +import { cancelSignal } from "./trigger/streams"; |
| 241 | + |
| 242 | +const app = express(); |
| 243 | +app.use(express.json()); |
| 244 | + |
| 245 | +app.post("/api/cancel", async (req, res) => { |
| 246 | + const { runId, reason } = req.body; |
| 247 | + |
| 248 | + await cancelSignal.send(runId, { reason }); |
| 249 | + |
| 250 | + res.json({ cancelled: true }); |
| 251 | +}); |
| 252 | +``` |
| 253 | + |
| 254 | +### Sending from another task |
| 255 | + |
| 256 | +You can send input stream data from one task to another running task: |
| 257 | + |
| 258 | +```ts |
| 259 | +import { task } from "@trigger.dev/sdk"; |
| 260 | +import { approval } from "./streams"; |
| 261 | + |
| 262 | +export const reviewerTask = task({ |
| 263 | + id: "auto-reviewer", |
| 264 | + run: async (payload: { targetRunId: string }) => { |
| 265 | + // Perform automated review logic... |
| 266 | + const isApproved = await performReview(); |
| 267 | + |
| 268 | + // Send approval to the waiting task |
| 269 | + await approval.send(payload.targetRunId, { |
| 270 | + approved: isApproved, |
| 271 | + reviewer: "auto-reviewer", |
| 272 | + }); |
| 273 | + }, |
| 274 | +}); |
| 275 | +``` |
| 276 | + |
| 277 | +## Error handling |
| 278 | + |
| 279 | +The `.send()` method will throw if: |
| 280 | + |
| 281 | +- The run has already completed, failed, or been canceled |
| 282 | +- The payload exceeds the 1MB size limit |
| 283 | +- The run ID is invalid |
| 284 | + |
| 285 | +```ts |
| 286 | +import { cancelSignal } from "./trigger/streams"; |
| 287 | + |
| 288 | +try { |
| 289 | + await cancelSignal.send(runId, { reason: "User clicked stop" }); |
| 290 | +} catch (error) { |
| 291 | + console.error("Failed to send:", error); |
| 292 | + // Handle the error — the run may have already completed |
| 293 | +} |
| 294 | +``` |
| 295 | + |
| 296 | +## Important notes |
| 297 | + |
| 298 | +- Maximum payload size per `.send()` call is **1MB** |
| 299 | +- You cannot send data to a completed, failed, or canceled run |
| 300 | +- Data sent before a listener is registered inside the task is **buffered** and delivered when a listener attaches |
| 301 | +- Input streams require [Realtime Streams v2](/tasks/streams#enabling-streams-v2) (enabled by default in SDK 4.1.0+) |
| 302 | +>>>>>>> theirs |
0 commit comments