AWS API Gateway V1 streaming
An example on how to enable streaming for API Gateway REST API routes.
api.route("GET /", { handler: "index.handler", streaming: true,});The handler uses the native awslambda.streamifyResponse and
awslambda.HttpResponseStream.from to stream responses through API Gateway.
export const handler = awslambda.streamifyResponse( async (event, stream) => { stream = awslambda.HttpResponseStream.from(stream, { statusCode: 200, headers: { "Content-Type": "text/plain; charset=UTF-8", "X-Content-Type-Options": "nosniff", }, });
stream.write("Hello "); await new Promise((resolve) => setTimeout(resolve, 3000)); stream.write("World");
stream.end(); },);const api = new sst.aws.ApiGatewayV1("MyApi");api.route("GET /", { handler: "index.handler", streaming: true,});api.route("GET /hono", { handler: "hono.handler", streaming: true,});api.deploy();import { Hono } from "hono";import { streamText } from "hono/streaming";import { streamHandle } from "hono/aws-lambda";
const app = new Hono().get("/hono", (c) => { return streamText(c, async (stream) => { await stream.writeln("Hello"); await stream.sleep(3000); await stream.writeln("World"); });});
export const handler = streamHandle(app);View the full example.