Sending ReadableStream from client component to server action #81514
Unanswered
CarterRoll
asked this question in
App Router
Replies: 1 comment 3 replies
-
Hi, Isn't the problem here the If we focus only on the const stream = new TransformStream();
const reader = stream.readable.getReader()
reader.read().then(function pump(args) {
if (args.done) return
console.log(args.value)
return reader.read().then(pump);
})
const texts = ["message 1\n", "message 2\n"];
const writer = stream.writable.getWriter();
for (const part of texts) {
await writer.ready;
await writer.write(part);
} This will print: message 1
message 2 But if we do: const stream = new TransformStream();
const reader = stream.readable.getReader()
await reader.read().then(function pump(args) {
if (args.done) return
console.log(args.value)
return reader.read().then(pump);
})
const texts = ["message 1\n", "message 2\n"];
const writer = stream.writable.getWriter();
for (const part of texts) {
await writer.ready;
await writer.write(part);
} Now we stopped the execution waiting for the reader to complete, but we never write to it... so we will never move on - it is a deadlock. One way would be to queue the writing like this: const stream = new TransformStream();
const texts = ["message 1\n", "message 2\n"];
const writer = stream.writable.getWriter();
texts.forEach(part => writer.ready.then(() => writer.write(part)));
const reader = stream.readable.getReader()
await reader.read().then(function pump(args) {
if (args.done) return
console.log(args.value)
return reader.read().then(pump);
}) This way, we fire each write operation, but move on synchronously to setup the reader, and only then the event loop would start to write. Technically, I am reading this correctly, the |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
As title says. Is this possible? I am trying to send over a large amount of data (50mb) from the client to the server to parse into a database. I can't send the file all in one go, as that far exceeds the 3mb limit of the serverless function. I implemented functionality to stream responses from the server to the client using ReadableStreams, which works great. I am now attempting to do the inverse, sending a ReadableStream from the client to the server, and then sending the file (in chunks, if necessary) through the stream to be parsed on the server. However, the second I try to pass a ReadableStream to a server action, none of the code in the server action gets executed. There is no error when I do this, either.
Code to repeat behavior:
Beta Was this translation helpful? Give feedback.
All reactions