Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for streaming #430

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

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for server-side events?
1 participant