Friday 6 July 2012

A WebSocket Cordova plugin for Android

This week I've investigated the feasibility of leveraging NullMQ inside a Cordova/PhoneGap application on Android. NullMQ sends and receives messages via WebSocket, and unfortunately, Android's browser doesn't support the standard yet. I found Cordova plugins on GitHub that implement WebSocket. The problem is that none of them functioned properly against the latest version of the WebSocket spec, that is, RFC 6455. So I decided to fork the Java-WebSocket project and expose it as a Cordova plugin. The cool thing about the Java-WebSocket project is that it supports, in addition to RFC 6455, earlier releases of the spec. Although, by default, the plugin is configured to use RFC 6455.

The plugin's API is identical to the WebSocket one so you don't need to re-learn a new API or change existing WebSocket calls. To install:
  1. Include websocket.js in the your HTML file:

  2. From your console, enter 'mvn package' in the Java-WebSocket project root directory. Move the produced JAR to your Cordova application classpath.

  3. Add the following plugin entry to the plugins.xml file:

12 comments:

  1. Thanks for the phonegap plugin. I've followed your instructions above but I'm getting the following error in the WebSocket.js file:
    "Uncaught TypeError: has no methode 'exec' at file :///android_asset/www/js/wesocket.js:52"

    What am I missing? I'm using Cordova 1.9.0 in eclipse. Thanks again.

    ReplyDelete
    Replies
    1. Hi Steve, it looks like the Cordova JAR is not in the classpath.

      Delete
    2. Actually, have you included the Cordova JS file in the html file?

      Delete
  2. Hi Claude, Thank for your reply. Yes, I have the Cordova jar file in my class path and the Cordova-1.9.0.js file included in the html. I've put the Cordova js file in the js folder as well as in the root path and even though I change the path in the html file to match the Cordova js file I still get this error. I'll double check the jar file location but it is show in my included jar files in Eclipse. Thanks again for your help.

    ReplyDelete
    Replies
    1. The error seems to point that Cordova is not defined. Do you load the Cordova-1.9.0-js file before you load websocket.js?

      Delete
    2. yes I include Cordova-1.9.0.js before I include websocket.js. Now I using websocket.js without flash so I not using web_socket.js. I don't want to use flash. Could my system be compiling the cordova plugin as a java jar file and not an android jar file?

      Delete
  3. Hey, thanks for plugin!

    Does it support secure connections (wss://). I've tried it without a luck. Could you please help?

    ReplyDelete
  4. Any chance getting this to work with cordova-2.2?

    ReplyDelete
  5. I was having an issue send over json message containing ' in the data, the ' will break the loadUrl in onMessage

    i.e.

    ReplyDelete
  6. In Cordova /res/xml/plugins.xml has been renamed to res/xml/config.xml

    ReplyDelete
  7. Is this plugin compatible with Cordova 2.6? If not are there any alternatives the you are aware of?

    ReplyDelete
  8. Is therer any chance to support Cordova 3.3.0+??

    ReplyDelete