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

bug: CapacitorHttp - No support for Server-Sent Events (EventSource API) #6582

Open
torte opened this issue May 10, 2023 · 15 comments
Open

bug: CapacitorHttp - No support for Server-Sent Events (EventSource API) #6582

torte opened this issue May 10, 2023 · 15 comments
Labels
platform: android platform: ios type: feature request A new feature, enhancement, or improvement

Comments

@torte
Copy link

torte commented May 10, 2023

Bug Report

Capacitor Version

@capacitor/cli: 4.7.0
@capacitor/core: 4.7.0
@capacitor/ios: 4.7.0
@capacitor/android: 4.7.0

Platform(s)

iOS and Android regardless of version

Current Behavior

When using the web's EventSource API for use with Server-Sent Events, the CapacitorHttp plugin won't be used and instead it falls back to the web's native implementation. This is caused CORS issues when intending to use device's native HTTP handling (hence the usage of CapacitorHttp).

Expected Behavior

As with fetch and XMLHttpRequest, EventSource should also be patched and work through device native events.

Additional Context

Likely related to other issues similar to this since the Content-Type is text/event-stream for Server-Sent events: #6299 & #6206 ?

@ionitron-bot ionitron-bot bot added the triage label May 10, 2023
@IT-MikeS IT-MikeS added the type: feature request A new feature, enhancement, or improvement label May 11, 2023
@ghost
Copy link

ghost commented Jun 14, 2023

I tried to use https://github.com/Azure/fetch-event-source to get a fetch() based approach for text/event-stream, but even that isn't good enough, since the patched fetch can't seem to stream data.

@youngerjo
Copy link

I'd like to kindly request the Ionic team to consider adding support for the text/event-stream response type. This would greatly benefit scenarios like integrating OpenAI's chat completion with streaming, which is currently not achievable using the Capacitor's HTTP module. Thank you for considering this!

@ghost
Copy link

ghost commented Sep 7, 2023

@youngerjo the type is not enough. the entire http client needs to be restructured to support streaming data. I wonder if they should adopt OkHttp instead of trying to write it themselves

@youngerjo
Copy link

@jrobeson Thanks for pointing that out. For the time being, I might lean towards using the web fetch method for streaming responses. My main concern with that approach is the limitation of not being able to include an authorization header due to CORS restrictions. I genuinely hope the team considers restructuring the http client or integrating a more efficient solution like OkHttp in the future.

@torte
Copy link
Author

torte commented Sep 8, 2023

Yeah, CORS has also been the main problem with our app just using native fetch. For just HTTP with simple content-types like application/json or text/html the current http plugin works fine, but everything else it does not. For now can not use SSE at all in the mobile app OR would need to make my app insecure essentially.

@nimo23
Copy link

nimo23 commented Sep 8, 2023

In #5145 (comment), nothing was said about SSE and it may be that it was simply forgotten at the time or there were other reasons why it was not included in capacitorHttp.

@thomasvidas, can you tell us whether SSE was even planned or whether it is even possible to include this?

Especially for mobile applications, SSE is a much more energy-saving alternative to "fetch with polling", for example. Btw, in contrast to the use of the "web's native implementation", capacitorHttp can even improve SSE communication by

@maelp
Copy link

maelp commented Dec 14, 2023

Is there also a way to use streaming HTTP request? I can do it from my web-app using fetch with a keep-alive connection, but it doesn't work once compiled to Capacitor

@IsaSalemHame
Copy link

Any update?

1 similar comment
@jaspart
Copy link

jaspart commented Mar 28, 2024

Any update?

@th1nkgr33n
Copy link

Any Update?

@klauss194
Copy link

such a pitty that capacitor team wont reply to all these requests.
Guys I highly recommend if possible -> just move to a pwa. apple its not killing them anymore

@pizzaeater
Copy link

I'm missing support of SSE in the CapacitorHttp. Could we get any updates regarding this feature, please? Thank you.

@mdledoux
Copy link

mdledoux commented Jun 26, 2024

What is the reason for even needing support in CapacitorHttp? I actually use Angular and use @angular/http (HttpClient) in all of my services - so I haven't really needed the Capacitor plugin, which only a couple years ago was still a community plugin.

I have an assignment to implement SSE in my Angular app, and my plan was to write a service that directly uses the EventSource API, but then I discovered that doesn't take headers. So I did some digging, and my plan is to use the EventSourcePlus module in my service (in order to add Auth headers to it). Is there something wrong with this, or is this more of an issue for other frameworks which don't have a built-in HTTP client? (though I would have assumed that React and Vue devs are just using Axios - same question I suppose: What is the reason for not using Axios (or even fetch) in an Ionic+Capacitor app?

@mdledoux
Copy link

mdledoux commented Jun 26, 2024

I'll also out that since this issue was opened in May of 2023 (using Cap 4.7.0), Capacitor 5 came and went, and Capacitor 6 came out April 15, 2024, and Cap 6.1 came out June 11, 2024. I'd be curious if anyone has better luck with these in the latest version.

Update:
Sadly, I see nothing about SSEs or EventSource in the CHANGELOG

@torte
Copy link
Author

torte commented Jun 26, 2024

@mdledoux You are right, you can achieve the same with regular web http client libraries and the EventSource API, but with caveats. One main caveat for me was that (at least at that time when I opened the ticket) you had to apply an open CORS policy on your backend, because the web API's run from the Capacitor mobile app don't set a trustworthy origin header (localhost isn't trustworthy on a production environment). There seems to be an option to overwrite that, but that didn't work for me, so I ended up trying CapacitorHttp and ran into more fundamental issues there (hence the ticket).

Maybe the soluition is to be actually able to configure the Origin header instead of "falling" back to the native HTTP clients as CapacitorHttp does. That being said, setting an arbitary Origin header should also not be allowed in your config, because you would be able to hack other sites like this. So I guess the whole thing is a bit more complicated and there would be at least some validation process needed to achieve this (kind of how you validate your website with Android/iOS deep linking).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: android platform: ios type: feature request A new feature, enhancement, or improvement
Projects
None yet
Development

No branches or pull requests