Socket Programming : How to Integrate Socket.io java Client in your Android App without Maven

socket

I needed to implement Websockets between my android app and a web application to efficiently stream real-time text data (values that change over 10 times per second .. or more). Normally, most applications connect to a server using http calls which establishes a connection to the server, processes the request, provides a response and closes the connection. This is a problem for high-speed streaming requirements as new connections need to be established for each “chunk” of data sent. This is both wasteful and slow. What I needed was a protocol to implement continuous streaming!

Enter Websockets protocol, which enables full duplex continuous exchange between a server and a client (web-browser, mobile device) over a single TCP connection [See this article for more on how Sockets Work].  Also enter Socket.io which is an awesome Node.js extension that implements the WebSockets protocol in an easy to use manner. Amazing!!

Luckily there is also a Socket.io client port in android but it has dependencies which are designed to be installed using the Maven (or Gradle) build tool. Bummer!

I have minimal Maven experience (and I don’t use Gradle at this moment) and did spend the greater part of a day struggling to get my eclipse android application to build (compile and install dependencies) using Maven. Each time I added a Maven Nature to my android project, the compile process got garbled and I was left with the following error …

06-14 18:39:53.185: E/AndroidRuntime(5551): Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.example.sockettest.MainActivity” on path: DexPathList[[zip file “/data/app/com.example.sockettest-2/base.apk”],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

For some reason, my android activity classes were not being included in my apk. The above might have been resolved by a more careful construction of my pom.xml file (see this SO post).  I was also reluctant to change my build system from the android developer tool plugin to Maven. My work around was as follows.

1. Create a (separate) default maven project in eclipse and add the socket.io maven path in the dependencies section of your  pom.xml file.

<dependencies>
  <dependency>
    <groupId>com.github.nkzawa</groupId>
    <artifactId>socket.io-client</artifactId>
    <version>0.5.1</version>
  </dependency>
</dependencies>

You may need to install the maven eclipse plugin if you don’t already have it installed. Execute Maven install and this will download all the socket.io dependency jars you need to your local maven repository. By default, your Maven local repository is a .m2 folder :

  • Unix/Mac OS X – ~/.m2
  • Windows – C:\Documents and Settings\{your-username}\.m2

2. Using the dependency list, find all of the downloaded jars and then copy them to your android project lib directory.

libs

3. Now add these jar files to your android build path . Select all the jars, right click > Build Path > Add to Build Path. This should add all the necessary files to your project, and you are all set to use Socketio calls in your android project – connect to a socket, listen for messages, broadcast messages etc. And hey presto! You are all good to go .

Working with Socket.io has been a most interesting endeavour, and so far I am simply amazed by the speed and sheer genius of Socket Programming. Socket.io truly rocks!

About Vykthur

Mobile and Web App Developer and Researcher. Passionate about learning, teaching, and recently - writing.
This entry was posted in Tutorials and tagged , . Bookmark the permalink.