How to send ReadableStream through trpc

I’m using OpenAIs API and they have the option to send ReadableStream to send chunks of data by enabling the stream option. We all know that TRPC can only pass json from backend to frontend. Just wondering if it’s possible to pass a ReadableStream? Is it serializable?
4 Replies
jingleberry
jingleberry2y ago
If I get this correctly, you have a trpc function which makes a request to OpenAPI and receives a ReadableStream back. You want to then pass this back to the frontend? If so, why not wait for all the data to be streamed. Convert it into a string or whatever it is. Then you can return it as JSON to your frontend. My initial assumption is no. How can you pass the contents of a readable stream into json unless you’ve already wanted for the stream to complete? Or did they want to pass a reference (or just something) to the client and let the client consume the stream somehow? I feel this would be possible using api routes, but probably not trpc as it is today. (Happy to be prove wrong tho)
Typedef
Typedef2y ago
It is in fact possible with API routes ref: https://vercel.com/blog/gpt-3-app-next-js-vercel-edge-functions. I'm asking for TRPC because I don't know if there's actually a way.
jingleberry
jingleberry2y ago
There’s probably not a way via trpc. You’ll need to be all or nothing in regards to returning data as a stream. Plus, consumption of the stream will also need to be handled on the frontend differently as well Best idea to use the api route. And it seems like vercel has a pretty nice example already
Typedef
Typedef2y ago
You’re prob right. Trpc does not even support multipart/form so I doubt they’ll be able to support streams
Want results from more Discord servers?
Add your server