0

I was wondering if there was a way to create a bi-direction stream (or allow multiple HTTP body sends over a single long-polled connection) in OKHTTP3 that does not require data to be constantly flowing between the client and the server.

For context, I am trying to implement a system where there can be intermittent data pushes can occur from either the client or the server over a persistent connection. The application is data-use sensitive, so I don't want the client sending requests to the server to see if there is data ready, I just want the server to push it.

Jeremy Ir
  • 1
  • 1

1 Answers1

0

A Websocket connection is the ideal solution to your problem. This creates a persistent connection between the client and the server and both parties can start sending data at any time.

in OKHTTP you can implement this by

adding the library to your build gradle file compile 'com.squareup.okhttp3:okhttp:3.6.0'

Create a class that implements the okhttp WebsocketListener interface

private final class MyWebSocketListener extends WebSocketListener {

  private static final int CLOSE_STATUS = 1000;
  @Override
  public void onOpen(WebSocket webSocket, Response response) {
    webSocket.send("Hello");
    webSocket.close(CLOSE_STATUS, "Goodbye");
  }
  @Override
  public void onMessage(WebSocket webSocket, String text) {
    log(text);
  }
  @Override
  public void onMessage(WebSocket webSocket, ByteString bytes) {
    log(bytes.hex());
  }
  @Override
  public void onClosing(WebSocket webSocket, int code, String reason) {
    webSocket.close(CLOSE_STATUS, null);
    log("Closing");
  }
  @Override
  public void onFailure(WebSocket webSocket, Throwable t, Response response) {
    log(t.getMessage());
  }
}

Create a method to initiate the connection

  private void connect() {
    Request request = new Request.Builder().url("ws://my.websocket.url").build();
    MyWebSocketListener listener = new MyWebSocketListener();
    WebSocket ws = client.newWebSocket(request, listener);
    \\ to shutdown the connection client.dispatcher().executorService().shutdown();
  }

This should establish a connection with the server and should persist as long as the application is alive. I recommend reading more on websockets if you are the same person responsible for the backend.

Kofi Nyarko
  • 253
  • 2
  • 6
  • I should have clarified a bit more, but it needs to be over HTTP; WebSockets is not an option. There is an existing backend we need to interact with. – Jeremy Ir Oct 16 '18 at 20:11
  • In that case I recommend using Retrofit Observables which have great utility methods for pulse polling the api. I found this link to explain it better. https://stackoverflow.com/questions/28369689/android-polling-a-server-with-retrofit/28398332 . hope it helps. – Kofi Nyarko Oct 16 '18 at 20:33
  • Polling isn't a true push solution however, it still requires the client to initiate the communication. The use case I'm looking at demands near-real time delivery of data from the server (though the frequency of data being available to transmit may very from frequent to once per month), so the polling rate would need to be high, meaning that I'd have to poll a lot and potentially get false hits. It's sounding like OKHTTP may not have this style of functionality available, yes? – Jeremy Ir Oct 16 '18 at 21:57
  • HTTP/2 supports this natively but I have yet to find out how to implement this in Java or Kotlin – Simao Gomes Viana Mar 08 '23 at 11:59