This package is inspired by Vercel's take on Resumable Streams used in the Chat SDK, except instead of Redis, this relies on S2 providing a basic implementation to create and resume streams.
To use this package, you need to:
-
Create an Access token for S2 and a
Basin
to store all your streams. You can do so by signing up here. Set the created token asS2_ACCESS_TOKEN
in your env. -
Create a new basin from the basins tab with appropriate
retention age
for streams and thecreate-on-append
option turned on, and set it asS2_BASIN
in your env.
The incoming stream is batched and the batch size can be changed by setting S2_BATCH_SIZE
.
To integrate this package with the Chat SDK, checkout the following changes here.
import { createResumableStreamContext } from "resumable-stream";
import { after } from "next/server";
const streamContext = createResumableStreamContext({
waitUntil: after,
});
export async function POST(req: NextRequest, { params }: { params: Promise<{ streamId: string }> }) {
const { streamId } = await params;
const inputStream = makeTestStream();
const stream = await streamContext.createNewResumableStream(
streamId,
inputStream,
);
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
},
});
}
export async function GET(req: NextRequest, { params }: { params: Promise<{ streamId: string }> }) {
const { streamId } = await params;
const stream = await streamContext.resumeExistingStream(
streamId
);
if (!stream) {
return new Response("Stream is already done", {
status: 422,
});
}
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
},
});
}