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: