kit icon indicating copy to clipboard operation
kit copied to clipboard

Add Streaming Support to Remote Functions

Open shlyk opened this issue 6 months ago • 4 comments

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

shlyk avatar Aug 01 '25 11:08 shlyk

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 🤔.

realfakenerd avatar Aug 01 '25 12:08 realfakenerd

Realtime probably will be added via sse

david-plugge avatar Aug 02 '25 11:08 david-plugge

Reference to Rich talking about future possible plans for SSE events for streaming: https://youtu.be/e-1pVKUWlOQ?t=3166

Maxiviper117 avatar Aug 12 '25 14:08 Maxiviper117

It's quite advance topic. TenStack does quite nicely

gevera avatar Dec 04 '25 21:12 gevera