Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Add support for streaming #430

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add support for streaming #430

wants to merge 3 commits into from

Conversation

arcticfly
Copy link

@arcticfly arcticfly commented Dec 20, 2023

Fixes #429

tRPC is really awesome, but it currently doesn't support or allow server-sent events. This small PR doesn't affect behavior for existing supported requests, but does allow a route to return a readable stream, whose events will be piped to the client.

With this change, routes will be able to return streams and pipe events through like so:

const responseStream = new TransformStream();
const writer = responseStream.writable.getWriter();
const encoder = new TextEncoder();

void writer.write(encoder.encode(JSON.stringify(chunk1)));

// Send one chunk every second for 5 seconds
let count = 0;
const interval = setInterval(() => {
  void writer.write(encoder.encode(JSON.stringify(chunk2)));
  if (count++ === 5) {
    void writer.close();
    clearInterval(interval);
  }
}, 1000);

return responseStream.readable;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for server-side events?
1 participant