Add Streaming Support to Remote Functions
Describe the problem
Hi SvelteKit team! Remote functions are awesome for server-client calls. I think adding a stream primitive to $app/server for generators to return ReadableStream would be great for things like real-time data or large datasets.
Describe the proposed solution
A stream export that turns generator yields into a JSON stream, consumable as an async iterable on the client.
Server (src/lib/server/todos.remote.ts):
import { stream } from '$app/server';
export const watchTodos = stream(async function* () {
yield { id: 1, title: 'Todo 1', completed: true };
await new Promise(r => setTimeout(r, 1000));
yield { id: 2, title: 'Todo 2', completed: false };
});
Client (src/routes/todos/+page.svelte):
<script lang="ts">
import { watchTodos } from '$lib/server/todos.remote';
</script>
<ul>
{#each watchTodos() as todo}
<li>{todo.title} - {todo.completed ? 'Done' : 'Pending'}</li>
{/each}
</ul>
- Could add cancellation or custom serialization.
- Please close if this duplicates another issue.
Thanks for considering!
Alternatives considered
Manual ReadableStream in a +server.ts route: Verbose, requires custom fetch handling on the client, and doesn't integrate with remote functions' type safety and simplicity.
// +server.ts
export async function GET() {
return new Response(new ReadableStream({
async start(controller) {
controller.enqueue(JSON.stringify({ id: 1, title: 'Todo 1' }));
// More enqueues...
controller.close();
}
}), { headers: { 'Content-Type': 'application/json' } });
}
Importance
would make my life easier
Additional Information
No response
That would help me with a big csv. I liked the stream() and hope Rich and the guys considerate your suggestion. But i don't know how it could work with real time data 🤔.
Realtime probably will be added via sse
Reference to Rich talking about future possible plans for SSE events for streaming: https://youtu.be/e-1pVKUWlOQ?t=3166
It's quite advance topic. TenStack does quite nicely