Vercel Streaming Quickstart
Learn how to stream responses from Serverless and Edge Functions to provide an improved user experience.In this quickstart, you'll learn how to get started with streaming on Vercel. The guide will cover:
- Creating a Vercel Function
- Using the Web Streams API to stream an API response
- Running your function locally
-
You should have the latest version of Vercel CLI installed. To check your version, use
vercel --version
. To install or update Vercel CLI, use:pnpm i -g vercel@latest
-
You should be using Node.js 18 or later.
-
You should have an existing project. If you don't have one, you can run the following terminal commands to create a Next project:
pnpm i next
npx create-next-app@latest
Vercel supports streaming responses from Vercel Functions, which you typically create within your framework code, such as with a Next.js Route Handler. Both the nodejs
runtime and the edge
runtime supports streaming by default. This guide will show you how to create a streaming Function using the Node.js runtime.
- To get started, first create
app/api/streaming-example/route.ts
- Then, add the following code, which sets up a basic Function with a route handler method:
app/api/streaming-example/route.tsexport const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // always run dynamically export async function GET() { // Streaming code will go here }
- To get started, first create
This example will demonstrate a basic example of streaming on response from your function. Add the following code to your function:
app/api/streaming-example/route.tsexport async function GET() { const customReadable = new ReadableStream({ start(controller) {...}, }); }
Use a
TextEncoder
to emit a stream of text as UTF-8 bytes:app/api/streaming-example/route.tsexport async function GET() { const encoder = new TextEncoder(); const customReadable = new ReadableStream({ start(controller) { controller.enqueue(encoder.encode('Basic Streaming Test')); // Prevent anything else being added to the stream controller.close(); }, }); }
Next, start producing a streaming response from your function:
app/api/streaming-example/route.tsexport async function GET() { ... return new Response(customReadable, { headers: { 'Content-Type': 'text/html; charset=utf-8' }, }); }
Finally, the whole file should look like this:
app/api/streaming-example/route.tsexport const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // always run dynamically export async function GET() { // This encoder will stream your text const encoder = new TextEncoder(); const customReadable = new ReadableStream({ start(controller) { // Start encoding 'Basic Streaming Test', // and add the resulting stream to the queue controller.enqueue(encoder.encode('Basic Streaming Test')); // Prevent anything else being added to the stream controller.close(); }, }); return new Response(customReadable, { headers: { 'Content-Type': 'text/html; charset=utf-8' }, }); }
Use
next dev
to start a local development server:terminalnext dev
Navigate to http://localhost:3000/api/streaming-example to see the streamed response from your route.
The page should display the message "Basic Streaming Text".
Was this helpful?