Skip to content

Add ai-streaming-ndjson example#1488

Open
guuszz wants to merge 2 commits into
vercel:mainfrom
guuszz:feat/ai-streaming-ndjson-example
Open

Add ai-streaming-ndjson example#1488
guuszz wants to merge 2 commits into
vercel:mainfrom
guuszz:feat/ai-streaming-ndjson-example

Conversation

@guuszz

@guuszz guuszz commented May 28, 2026

Copy link
Copy Markdown

What is this?

A new example under solutions/ai-streaming-ndjson that demonstrates progressive token-by-token streaming from a Next.js Route Handler to the browser using NDJSON (newline-delimited JSON) over a ReadableStream.

It fills a gap: most streaming examples use Server-Sent Events, but EventSource is GET-only and locked to text/event-stream. For many real-world cases (AI completions with long prompts, long-running ops with progress, multi-event responses) you want a POST with a body — and NDJSON delivers the same UX with less ceremony.

What's included

  • app/api/stream/route.tsReadableStream writing one JSON event per line with typed events (meta, chunk, done, error). Edge-compatible.
  • app/page.tsx — client component that reads via getReader() + TextDecoder, parses NDJSON line-by-line, and renders chunks progressively with a live progress bar and typing cursor.
  • README.md — explains the why (EventSource limitations), the protocol, server/client snippets, when to use vs WebSockets/SSE, and the X-Accel-Buffering header.

How to test

  1. pnpm install
  2. pnpm dev and open http://localhost:3000
  3. Hit "Start streaming" — words appear one at a time
  4. Or test the endpoint directly:
This example demonstrates progressive token-by-token streaming from a
Next.js Route Handler to the browser using NDJSON (newline-delimited JSON)
over a ReadableStream — a simpler alternative to Server-Sent Events when
you need POST with a body.

What's included:
- app/api/stream/route.ts: ReadableStream writing one JSON event per line
  with custom types (meta, chunk, done, error). Edge-compatible.
- app/page.tsx: client component that reads the stream via getReader() +
  TextDecoder, parses NDJSON line-by-line, and renders chunks progressively
  with a live progress bar and typing cursor effect.
- README.md: explains the why (EventSource doesn't accept POST + body),
  the protocol, server/client snippets, when to use vs WebSockets/SSE,
  and X-Accel-Buffering header notes.

The endpoint accepts { prompt } and echoes it back word-by-word with a
40ms delay to simulate token generation — in a real app, replace the loop
with your LLM SDK's stream iterator (OpenAI/Anthropic/Gemini all have this).

Why this matters: many AI integrations need a POST with a long prompt or
context document, but EventSource is GET-only. Most tutorials show SSE
demos that conveniently fit in a query string. This example fills the
gap with a production-tested pattern.
@vercel

vercel Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

@guuszz is attempting to deploy a commit to the Vercel Examples Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mintlify-docs-rewrite Error Error May 28, 2026 1:48am
@vercel

vercel Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Deployment failed with the following error:

The `vercel.ts` schema validation failed with the following message: `rewrites[0]` missing required property `destination`

Learn More: https://vercel.com/docs/concepts/projects/project-configuration

Comment thread solutions/ai-streaming-ndjson/app/api/stream/route.ts Outdated
Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant