Search code examples
javaandroidserverfull-duplex

Bi-directional HTTP1 full duplex streaming on Android


I have an http1 API that I have written (in Golang) code ex here that I have gotten to work perfectly on iOS code ex here

But I cannot seem how to figure out how to implement it on Android.

The API is atypical in that instead of one server/response pair, any number of them can happen. It is so I can upload pieces of a db, get confirmation, and continue. Similarly data can come down from the website to client. When the iOS client connects I can see the connection gets upgraded, etc... and everything works as expected.

First I tried to implement this using okHTTP web sockets classes. But this does not work because web sockets are considered to be "GET" and the API is "POST". Testing with changing the API to "GET" just to see what happens resulted in the ws library complaining about missing upgrade and connection headers, so I don't believe that's the answer.

Looking into okHTTP further, they have an "isDuplex" method I can override, but the docs plainly state that this only works on http2.

Looking at URLConnection classes and such, they claim to only support one request/body pair per call and will terminate after that, so seems like they won't work.

Any experience with this?


Solution

  • I was able to get it working, but there were issues I couldn't quite figure out where some users had difficulty with the API. It could have been a fluke, but we decided to limit the exposure to unknown problems and do it a traditional way.

    For anyone else that wants to achieve this, I've put a code example and a mention about the problems you might face in this repo:

    https://github.com/StainlessStlRat/FullDuplexNettyExample