Okhttp: Web Socket support?

Created on 21 May 2013  Â·  24Comments  Â·  Source: square/okhttp

Any chance we could se support for Web Sockets?

enhancement

Most helpful comment

okhttp-ws module shipped!

All 24 comments

Yeah, WebSockets are certainly appropriate as a feature for OkHttp. We haven't had a need for them so it's unlikely we'll do a great job prioritizing them. But it should be pretty straightforward to build on top of our current infrastructure.

public class WebSocket {
  WebSocket() { ... }
  public Headers requestHeaders();
  public void open();
  public Headers responseHeaders();
  public InputStream in() { ... }
  public OutputStream out() { ... }
}

Interested in contributing something? The way to implement this would be to reuse our Connection class but not HttpURLConnectionImpl or anything else.

Playing around with this. Here's my latest:

public final class OkWebSocketClient {
  public void connect(Request request, WsCallback callback) {
    // TODO
  }

  public interface WsCallback {
    void onConnect(Response response, BufferedSink sink);
    void onDisconnect(int code, String reason, IOException error);
    void onMessage(Buffer buffer);
  }
}

Websockets are bidirectional? So you'll need a way to send a message also?

... or does the sink do that? That's neat.

Yeah and then you .close() to disconnect as the client. Not sure how I feel about the abuse of BufferedSink for this yet.

Do websocket messages self-delimit? Is receiving [ABC, DEF] the same as receiving [ABCDEF] ?

It's a framed protocol with a payload length: http://tools.ietf.org/html/rfc6455#section-5.2

Here's a less sink-abusive version:

public final class OkWebSocketClient {
  public void connect(Request request, WsCallback callback) {
    // TODO
  }

  public interface WsConnection {
    void send(Buffer buffer);
    void close();
  }

  public interface WsCallback {
    void onConnect(Response response, WsConnection connection);
    void onDisconnect(int code, String reason, IOException error);
    void onReceive(Buffer buffer);
  }
}

Dig.

Maybe use the names WebSocket and WebSocketCallback ?

Split clean disconnects from failures?

Should sending a message be blocking or non-blocking? If non-blocking, should we offer a way to signal the last acked message when we're disconnected?

Should close() be blocking or non-blocking?

Do web sockets use the words message ? What's the word for the blobs that get sent?

public final class OkHttpClient {
  ... 
  public void connect(Request request, WebSocketCallback callback);
}

public interface WebSocket {
  /** Enqueues message to be sent. */
  void enqueue(Buffer message);
  void close();
}

public interface WebSocketCallback {
  void onConnect(WebSocket webSocket, Response response) throws IOException;
  void onDisconnect() throws IOException;
  void onFailure(Request request, Throwable throwable);
  void onMessage(WebSocket webSocket, Buffer message);
}

Do web sockets use the words message? What's the word for the blobs that get sent?

Frames wrap a payload. The problem is that you can break up the payload into multiple frames, potentially infinite.

From a consumer standpoint, I'd say you either are interested in the per-frame payload to handle the continuations (if present) yourself or you just want any continuations buffered and receive a contiguous payload.

Initial pass at an API (which deviates quite a bit from what's outlined here) in https://github.com/square/okhttp/pull/920. I'll be messing with web socket support in a websocket feature branch.

Love this! Can't wait to see this at some point in the future in master

FYI #1129

What does Square use for Websockets on Android?

We don't use WebSockets at this time.

So I guess Android development is not aligned with iOS development as they already have SocketRocket.

SocketRocket was not built for a production use-case. It serves an iOS-only, internal, development-only tool.

This is now on master but not yet part of the normal public API. You can create an instance by calling WebSocket.newInstance(OkHttpClient, Request) which gives you a instance that (after connecting) does synchronous writes and async reads+callbacks.

Please try this and report feedback!

I'm toying with the idea of an async writer since that's almost always what you want for clients like Android.

+1

I'm thinking to use okhttp for Socket.IO Java Client. https://github.com/nkzawa/socket.io-client.java
I'd like to know the status of the websocket implementation.

It's working, but not battle tested in the wild or with an API that we're
sure is good enough for public consumption. The classes are available and
usable in the latest snapshot.

On Sun, Mar 1, 2015 at 9:47 PM Naoyuki Kanezawa [email protected]
wrote:

+1

I'm thinking to use okhttp for Socket.IO Java Client.
https://github.com/nkzawa/socket.io-client.java
I'd like to know the status of the websocket implementation.

—
Reply to this email directly or view it on GitHub
https://github.com/square/okhttp/issues/193#issuecomment-76661734.

See the WebSocketEcho file in the samples for a quick crash course in how
to use it.

On Sun, Mar 1, 2015 at 10:36 PM Jake Wharton [email protected] wrote:

It's working, but not battle tested in the wild or with an API that we're
sure is good enough for public consumption. The classes are available and
usable in the latest snapshot.

On Sun, Mar 1, 2015 at 9:47 PM Naoyuki Kanezawa [email protected]
wrote:

+1

I'm thinking to use okhttp for Socket.IO Java Client.
https://github.com/nkzawa/socket.io-client.java
I'd like to know the status of the websocket implementation.

—
Reply to this email directly or view it on GitHub
https://github.com/square/okhttp/issues/193#issuecomment-76661734.

@JakeWharton thx, I will try.

okhttp-ws module shipped!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dannyZhou picture dannyZhou  Â·  3Comments

mleibner picture mleibner  Â·  3Comments

rfc2822 picture rfc2822  Â·  3Comments

yschimke picture yschimke  Â·  3Comments

yschimke picture yschimke  Â·  3Comments