Skip to content

How to return a stream? #12

Description

@cjkihl

How can I return a stream from better call?
I have the following streaming endpoint I would like to support for example:

return new Response(stream, {
				status: 200,
				headers: {
					"Content-Type": "application/x-ndjson",
					"Transfer-Encoding": "chunked",
					"Cache-Control": "no-cache",
					Connection: "keep-alive",
				},
			});

Suggestion:
One way to do it would be to use generators to yield the data back in a typesafe manner.

export const endpoint = createGeneratorEndpoint(
	"/ai/realtime",
	{
		method: "POST",
		requireRequest: true,
	},
	async function* (ctx) {
		for(const value of result) {
			// type safe value that is converted to a SSE (Server Sent Event)
			// and streamed to the client
			yield value;
		}
	},
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions