Add Streaming Support to Remote Functions

Open
#14,086 4 comments 39 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale

Research direction

Start by reading the remote-function entry points in the SvelteKit codebase, then compare the proposed server example in src/lib/server/todos.remote.ts with the client example in src/routes/todos/+page.svelte. Review the alternative +server.ts ReadableStream approach and determine the API, serialization, cancellation, and client-consumption requirements. Done would mean an agreed design and tests covering streamed remote-function values.

Written by the indexing model from the issue text.

Description

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

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sveltejs/kit

All issues in sveltejs/kit

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.