Saturday, March 26, 2011

Websockets on Android

Have you ever tried to use Websockets on Android? In a recent project that I have been working on, it was required to use WebSockets to build a native Android app. Why a native app? Well, the current webkit that ships with the Android devices is not as fast as we would have liked. So, we had to figure out a way to build a native app that would be able to connect through WebSockets.

There are quite a few libraries available out there. And it took a bit of time to figure out the best library available. Well, the library isn't that huge. The one that we finally decided to use had only 2 java files that got our work done. Simple, isn't it?

The link to the source is here. You just need these two files:
1. WebSocket.java
2. WebSocketFactory.java

Out of these two files, the main file that you would be working on is the WebSocket.java. The WebSocketFactory.java, is just an extension of the other class that gives you WebSocket instances, based one of the two Drafts (75/76) that you request for. I am using the Draft-76 of the implementation.

For our use, we didn't require the WebView portion embedded in the source. So we removed it.
This is how you instantiate and use a WebSocket.
WebSocket webSocket = new WebSocket(URI.create("ws://yourserver.com"), DRAFT-76, "any_id");
webSocket.connect();
After you connect to the server, there are different event callbacks that you could listen for.
  1. onOpen() : This method will be called when the WebSocket is connected and ready for sending and receiving data.
  2. onClose() : This method will be called when the connection is terminated.
  3. onMessage() : This method will be called then a message is received over the socket.
  4. onError() : This method is used to notify any error message that might occur. According to the project doc on github, this currently doesn't work properly.
When you need to send a message to the server, the WebSocket class has a send method which takes in a String as the message.
webSocket.send("Your message");
 To know more about the project, visit anismiles' github project. Thanks anismiles. :)

13 comments:

  1. We're doing a bunch of websocket stuff at the mo mate, got a few web apps if you ping me I will show you

    ReplyDelete
    Replies
    1. Can you show me those examples, John? Thanks.

      Delete
    2. Can you show those examples to me, John? Thanks

      figi500(at)gmail(dot)com

      Delete
    3. Can you show those examples to me, John? Thanks

      go19(at)walla(dot)com

      Delete
  2. Wow, nice to hear that. I would doze off in a few minutes... Will talk to you later :)

    ReplyDelete
  3. we tried using websoockets with android browser but encountered a problem in that we sent very small packets and they bunched up in androids browser as it doesnt release the packets until it has 4k of data
    similar to this
    http://code.google.com/p/android/issues/detail?id=13044

    ReplyDelete
  4. Ah..I never tried it with the browser.

    ReplyDelete
  5. Good explanation, but could you provide a simple web app example? I have never used websockets and I need to use for my app, I have looked around and haven't seen any good tutorial on it. would please help with it?
    you can email me on billahill(at)gmail(dot)com

    ReplyDelete
  6. I've gotten https://github.com/clwillingham/java-socket.io.client (which is socket.io) up and running on Android eally quickly. Note that it depends on https://github.com/TooTallNate/Java-WebSocket for its Websocket implementation

    ReplyDelete
  7. I've gotten https://github.com/clwillingham/java-socket.io.client (which is socket.io) up and running on Android eally quickly. Note that it depends on https://github.com/TooTallNate/Java-WebSocket for its Websocket implementation

    ReplyDelete
  8. Hey,
    I'm trying to implement this in my application but I can't get it to work with an echo test (http://www.websocket.org/echo.html), I get a SocketException - permission denied. Do you have any idea why this is happening?

    Thanks,
    Ricardo Amendoeira

    ReplyDelete
  9. Nevermind my previous post, I solved it already (http://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application)... sorry :S

    ReplyDelete