question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Fetch API not working correctly when using ReadableStream

See original GitHub issue

While building a react app, where the front-end must constantly receive streaming data, I noticed that each stream chunk, is only technically received, after the whole stream data is transmitted, although I can chrome network tab that is being received in several chunks. This issue occurs during development, but after building the app for production it no longer occurs.

React version: 16.13.1

Steps To Reproduce

  1. Create a component that receives a stream of data (for this test a flask stream was used although the bug occurs with other sources )

Code example:

import React, { useEffect } from 'react';

function Page_api() {

    useEffect(() => {

        const url = '/api/stream'

        const consume = responseReader => {
            return responseReader.read().then(result => {
                if (result.done) { return; }

                // do something with the current chunk
                const chunk = result.value;
                console.log("Received Chunk");

                return consume(responseReader);
            });
        }
  
        fetch(url).then(response => {
            return consume(response.body.getReader());
        })
            .catch(console.log.bind(console));

    })
    return (
        <div>
            <p>API Testing Page</p></div>)
}


export default Page_api

Same code in codesandbox: https://codesandbox.io/s/5vtqo , a stream source is also provided as a flask server on the api.py file.

The current behavior

Upon Receiving stream, react can only process each chunk of it on the end of the stream while on development (i.e. using react-scripts start)

dev

The expected behavior

Upon Receiving stream, react can process each chunk as soon as it arrives (i.e. using react-scripts start) - just as it occurs in the built application

working

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
DonHaulcommented, May 1, 2020

After some further testings, it seems it is related to the fact that the client and the flask servers where on the same host, and not related to react at all, thank you all

1reaction
vkurchatkincommented, May 1, 2020

The problematic code is within useEffect. React just runs it and doesn’t affect in any other way. You don’t even call any React-related code (such as setState) from there. Then again, React doesn’t have any network-related code at all.

If you post this question to Stackoverflow, you’d surely get the help you need.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Retrieve data from a ReadableStream object? - Stack Overflow
I am using the Fetch API and I don't see this to be clear from the documentation. The body is being returned as...
Read more >
[Meta-Bug] Support ReadableStream as Request.body in fetch ...
This bug is about request body. The code is probably not working in firefox because we have not exposed ReadableStream by default yet....
Read more >
Implement ReadableStream for the Fetch API's Response body
Created a new bug entry for the work of implementing ReadableStream with constructor as defined in the Streams Standard. ... Renaming this issue ......
Read more >
Streaming requests with the fetch API - Chrome Developers
Streaming requests with the fetch API ... then the streaming request isn't working correctly. ... const stream = new ReadableStream({
Read more >
Web Streams Everywhere (and Fetch for Node.js) - CSS-Tricks
They are generally considered to be difficult to work with. ... the API of the browser standard, returns a node stream, not a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found