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

Question: Is it possible to use OkHttp as a network stack on Android? #224

Open
gajicm93 opened this issue Oct 1, 2019 · 4 comments
Open

Comments

@gajicm93
Copy link

gajicm93 commented Oct 1, 2019

And how complicated would it be to implement it? OkHttp also works on pure Java so it wouldn't be a problem for the library in general.

@elverkilde
Copy link
Contributor

That's an interesting question; we might get some advantages from using a fully fledged HTTP client, but on the other hand we only need a simple WebSocket.

There's also the problem (for this official library) that OkHttp only supports API levels 21 and above, which probably means it's not something we'll pursue.

Does this solve a specific problem?

@gajicm93
Copy link
Author

gajicm93 commented Oct 2, 2019

Ok let me expand on my question:

  1. OkHttp has native WebSocket support which might make everything easier: https://square.github.io/okhttp/4.x/okhttp/okhttp3/-web-socket/

  2. Only the latest 4.x version of OkHttp actually has API 21 limit, older branch 3.12.x which is still maintained with bug fixes practically supports all relevant versions of Android. As I said, the same version of OkHttp also works on pure Java so this could easily stay a pure Java library.

  3. And finally, I don't have a particular problem to solve (for now), but it's general practice for Android libraries like Glide, ExoPlayer, etc.., to have "modular network stack" support, where support for OkHttp can be added as a small optional Gradle dependency. This is very useful for Android applications which use OkHttp/Retrofit for most of their normal network requests, as it provides a consistent experience across multiple Android versions, which could otherwise differ significantly depending on Android version, etc..

@elverkilde
Copy link
Contributor

That makes a lot of sense, thank you for expanding. I am worried the current implementation is so tightly coupled with the low-level WS library that changing this would require a complete rewrite. But I'll look into it.

I don't think we really need pure Java support, this library mainly (only?) exists to support Android. Any change that would improve the experience on Android is welcome IMO.

@gajicm93
Copy link
Author

gajicm93 commented Oct 4, 2019

Yeah it would be great if WS and OkHttp implementation were modularized as optional components via interfaces.

If you need inspiration on the programming design pattern how to do this, take a look at these 2 implementations:

ExoPlayer: https://github.com/google/ExoPlayer/tree/release-v2/extensions/okhttp
Glide: https://github.com/bumptech/glide/tree/master/integration/okhttp3

These 2 implement integration only for standard one-off requests, but as I said, OkHttp also has a native API for WebSocket implementation which you could use.

I have a project coming up where I'd use Pusher WebSocket, and will try to play with this myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants