react-native icon indicating copy to clipboard operation
react-native copied to clipboard

fetch implementation does not support streams from the spec - needs implementation in React Native core

Open pcowgill opened this issue 4 years ago • 25 comments

React Native has not implemented fetch on top of native APIs, and the fetch polyfill built on top of XHR does not support streams where response.body is a getter for a ReadableStream.

React Native version:

0.61.4

Steps To Reproduce

Use fetch and see that response.body is undefined rather than a getter for a ReadableStream according the the fetch spec.

Describe what you expected to happen:

I expected for response.body to be defined.

Related issues

https://github.com/facebook/react-native/issues/9629 https://github.com/facebook/react-native/issues/12912 https://github.com/github/fetch/issues/746#issuecomment-573251497 (cc @MattiasBuelens)

pcowgill avatar Jan 11 '20 17:01 pcowgill

React Native also has a Canny board for feature requests I previously was unaware of. If you come across this GitHub issue and care about this feature being prioritized, please feel free to upvote it over there as well. Thanks!

https://react-native.canny.io/feature-requests/p/fetch-streaming-body

pcowgill avatar Jan 13 '20 17:01 pcowgill

Let's create a fork of github/fetch under the Facebook org so we can open pull requests against it without worrying about breaking legacy browsers that are using github/fetch.

As discussed here https://github.com/github/fetch/issues/746

cc @hugomrdias @cpojer

pcowgill avatar Jan 22 '20 20:01 pcowgill

Can we create that fork of github/fetch under the Facebook org?

It looks like improvements to the current fetch solution are getting lots of upvotes on Canny: https://react-native.canny.io/feature-requests/p/fetch-streaming-body https://react-native.canny.io/feature-requests/p/implement-fetch-using-native-implementations-instead-of-fetch-on-top-of-xhr

pcowgill avatar Mar 09 '20 22:03 pcowgill

The Facebook GitHub org only contains project managed by Facebook. It'll mean we have to take care of releases and everything. I'm not sure if that's a good idea, you'll probably be able to move faster on a separate org. What about react-native-community? @alloy can get you set up with that.

cpojer avatar Mar 10 '20 11:03 cpojer

@pcowgill If you’re interested in spear-heading that effort in the react-native-community org, please contact me at [email protected] to get the process started.

alloy avatar Mar 10 '20 12:03 alloy

@cpojer @alloy Thanks for getting back to me! I think that could work. @hugomrdias Do you think that will serve our needs?

We should still keep this issue open though, because eventually we'll want to have a spec-compliant implementation of fetch at the native level in React Native.

pcowgill avatar Mar 10 '20 15:03 pcowgill

Just created the fork and added you, @pcowgill https://github.com/react-native-community/fetch

alloy avatar Mar 10 '20 15:03 alloy

@alloy Thanks! After something is published from that fork and we've tested the polyfill thoroughly, we'll want to make a PR to use the new npm package here https://github.com/facebook/react-native/blob/507379f6faba37e483610e4c35033e7b8d260eb4/Libraries/Network/fetch.js#L16

If anyone else would like to collaborate on that fork, just comment here!

pcowgill avatar Mar 10 '20 16:03 pcowgill

Just created the fork and added you, @pcowgill react-native-community/fetch

can you please add me too, thanks

hugomrdias avatar Mar 10 '20 16:03 hugomrdias

@hugomrdias You should have an invite in your email inbox now. Thanks!

pcowgill avatar Mar 10 '20 16:03 pcowgill

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Jun 09 '20 01:06 stale[bot]

This issue still requires the community's attention. Thanks!

pcowgill avatar Jun 09 '20 15:06 pcowgill

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Sep 07 '20 15:09 stale[bot]

@pcowgill Interested in this issue. Looking from the surface, do you think it's beneficial to use a different polyfill for fetch?

hoangpham95 avatar Sep 12 '20 08:09 hoangpham95

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

stale[bot] avatar Dec 25 '20 16:12 stale[bot]

I am really interested in bringing spec-compliant fetch to react native. I think there's renewed interest in the OSS community and JS developers to standardise where possible as seen in adding fetch to node.js core and the work Cloudflare, Vercel, Shopify et al are doing in WinterCG.

A spec compliant fetch also aligns with React Native's goals IMO.

Has there been any discussion in how to bring spec compliant fetch to React Native? The polyfill fork is a good start but I'm not sure the caveats especially around performance and bundle size make it suitable for general adoption.

tom-sherman avatar May 31 '22 15:05 tom-sherman

It is also woth to mention, that with the current state of react natives fetch implementation the new graphql features @defer and @stream don't work. But that is probabbly more related to this issue: https://github.com/facebook/react-native/issues/12912

mauricedoepke avatar Apr 11 '23 00:04 mauricedoepke

any update?

codergautam avatar Aug 28 '23 19:08 codergautam

When generative AI became popular, SSE became the dominant API response when developing AI apps, so it became urgent to satisfy stream, otherwise a lot of AI apps would be lost, and people had to use ionic/flutter/native to realize this feature.

taixw2 avatar Aug 31 '23 10:08 taixw2

On iOS, I can successfully receive a text stream using https://github.com/react-native-community/fetch, and I suggest try this library

taixw2 avatar Sep 07 '23 03:09 taixw2

Thanks a ton!!

codergautam avatar Sep 07 '23 03:09 codergautam

Made a blog post just about how to implement this using the Community fetch polyfill (streaming works like a charm in RN now!)

https://blog.codergautam.dev/how-to-use-streaming-fetch/

EDIT- SEE MY NEW SO ANSWER FOR STEPS TO USE IT: https://stackoverflow.com/questions/56207968/stream-api-with-fetch-in-a-react-native-app/77089139#77089139

I'm using it for OpenAI API and works perfectly

codergautam avatar Oct 17 '23 21:10 codergautam

Made a blog post just about how to implement this using the Community fetch polyfill (streaming works like a charm in RN now!)

https://blog.codergautam.dev/how-to-use-streaming-fetch/

Hey man, the article link you sent doesnt't work. Do you have another one? 🙏 Solving the same problem you've done

sergei-stralenia avatar Nov 08 '23 19:11 sergei-stralenia

My bad, I recently took down my blog since it was not getting enough traffic (I started it as an experiment) Anyways you can see the usage on one of my Stackoverflow Answers: https://stackoverflow.com/questions/56207968/stream-api-with-fetch-in-a-react-native-app/77089139#77089139

codergautam avatar Nov 08 '23 19:11 codergautam

can someone help me, stream not working with my IOS simulator : https://github.com/react-native-community/fetch/issues/24

fukemy avatar Feb 23 '24 02:02 fukemy