which websocket library to use in android app

which websocket library to use in android app  using -'android,node.js,websocket'

I want to add a Service to my Android app which runs in the background holding a WebSocket connection (possibly over several hours or even days) and regularly sends some data to a server.

Now there seems to be a bunch of WebSocket libraries for Java, and I'm not sure which one I should use:

Description from GitHub: A barebones WebSocket client and server implementation written in 100% Java. http://java-websocket.org/
-- This one is linked in my first result of googling "android websocket". However, it has quite a few open issues, especially about SSL connections, and it doesn't seem to be actively maintained at the moment.
Description from GitHub: Asynchronous socket, http (client+server), websocket, and socket.io library for android. Based on nio, not threads.
-- Again many open issues, but seems to be activiley maintained/worked on.
Project Tyrus
Description from Website: JSR 356: Java API for WebSocket - Reference Implementation
-- This is made by Oracle. Not sure if it works in Android.
Jetty WebSocket Client API
Info from Website: Jetty also provides a Jetty WebSocket Client Library to write make talking to WebSocket servers easier.
-- Again: Not sure if it works in Android.
Description from GitHub: Bare minimum websockets (hybi13/RFC) client for Android
-- This one is used in schwiz/android-websocket-example, which is the accepted answer for the StackOverflow-question "How to make the Android device hold a TCP connection to Internet without wake lock?".
Description from GitHub:  WebSockets with fallback transports client library for Node.js, Android and Java http://async-io.org
Description from GitHub: High-quality WebSocket client implementation in Java.
Description from GitHub: An HTTP+SPDY client for Android and Java applications. http://square.github.io/okhttp/
-- It has a Websocket module.

In addition, there is a native socket.io client library for Android:

Description from GitHub: Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.

To use the socket.io Android client would be handy for me, because I plan to use nodejs/socket.io for the web frontend anyway. But the native client is quite young and has several open issues. And in addition to that, it is my understanding that an android app does not have any benefit of using the socket.io client library (apart from being compatible with socket.io 1.0 server), because WebSocket support can be assured at the client side.

My requirements are as follows:

Compatibility with Android API 9 and higher
Possibility to connect via SSL
Keep the connection for a long time without having to hold a permanent wakelock
Compatibility with an available nodejs websocket server implementation or with socket.io

Any suggestions which one is the right library for these requirements?

asked Sep 18, 2015 by balvant maurya
0 votes

2 Answers

0 votes

Some notes.

  • koush/AndroidAsync does not perform the closing handshake which is required by RFC 6455. See this for details.

  • Project Tyrus works on Android, but make sure that its license (CDDL 1.1 and GPL 2 with CPE) and its size (Reducing WebSocket client jar size with ProGuard) meet your requirements. Also note that Tyrus may throw an exception when a text size is large (it's probably a bug). See this for details.

  • A 2-year-ago email thread in jetty-users mailing list says "We currently have no Android compatible Jetty 9 WebSocket client. There are plans to attempt to backport the Jetty WebSocket Client from JDK 7 to JDK 5/6 for android use, but its a lower priority than finishing our implementation of JSR-356 Java WebSocket API (javax.websocket)." Jetty's current document about its WebSocket Client API does not mention anything about Android.

  • codebutler/android-websocket does not perform the closing handshake which is required by RFC 6455 and may throw an exception on close. See this.

Consideration Points

Consideration points in selecting a WebSocket client implementation written in Java:

  1. Compliance. Not a small number of implementations do not implement the closing handshake required by RFC 6455.
  2. Required Java version. Java SE 5, 6, 7, 8 or Java EE? Works even on Android?
  3. Size. Some implementations have many dependencies.
  4. wss support.
  5. HTTP proxy support.
  6. wss over HTTP proxy support.
  7. Flexibility on SSL configuration. SSLSocketFactory and SSLContext should be able to be utilized without unnecessary restrictions.
  8. Custom HTTP headers in the opening handshake, including Basic Authentication.
  9. Custom HTTP headers in HTTP proxy negotiation, including authentication at the proxy server.
  10. Capable of sending all the frame types (continuation, binary, text, close, ping and pong) or not. Most implementations do not provide developers with means to send fragmented frames and unsolicited pong frames manually.
  11. Listener interface to receive various WebSocket events. A poor interface makes developers frustrated. A rich interface helps developers write robust applications.
  12. Able to inquire WebSocket state or not. RFC 6455 defines CONNECTING, OPEN, CLOSING and CLOSED states, but few implementations maintain their internal state transition in the defined way.
  13. Able to access the underlying raw socket.
  14. Intuitive easy-to-use API or not.
  15. Well-documented or not.
  16. Redirection (3xx) support.
  17. Digest Authentication support.

nv-websocket-client covers all the above except the last two. In addition, one of its small but convenient features is to send ping/pong frames periodically. It can be achieved just by calling setPingInterval/setPongInterval methods (See JavaDoc).

answered Sep 18, 2015 by balvant maurya
0 votes

Some other considerations:

Tyrus works on Android. However, the SSL libraries it uses in Android 5.0 are buggy and fail SSL handshakes. This is supposed to be fixed in newer versions of Android, but with the way that Android is not updated on many devices, this may be a problem for you.

Depending on how SSL is implemented for other websocket implementations, this may also be a problem.

AndroidAsync does not have this SSL issue. It does have other issues like not being able to set timeouts.

answered Sep 18, 2015 by param.oncemore