Android WebSocket Support

Diffusion 5.9

Diffusion 6.0

Diffusion 6.1

Diffusion 6.2

Diffusion 6.3

Diffusion Cloud

The following article is provided on an advice basis only. This article references 3rd party software that may require licensing.

In Android 4.3 and below, WebSockets are not natively supported. This means that the only transport mechanism available to Android devices running these versions is XHR, however this has cross domain, and performance implications. To alleviate these drawbacks this guide provides an example for enabling WebSockets on Android where there is no native support.

Cordova / PhoneGap

An external library is required to add WebSocket support when using Cordova or PhoneGap. There are many libraries available, however this example will use the WebSocket-for-Android (opens in new window – external site) library by knowledgecode.

Adding External Library

Browse to the directory for your Cordova / PhoneGap project in your command line interface.

Run one of the following commands for your environment.

$ cordova plugin add
$ phonegap plugin add

When installing this plugin, it adds the INTERNET permission to AndroidManifest.xml. If the plugin is removed, the permission is also removed at the same time, even if it is required for other plugins.

Using Library

The following function creates the WebSocket connection. It also includes callbacks for the connection status and incoming messages. This will not be used to send messages to Diffusion, it will just be used to create the connection to your Diffusion server.

The WebSocket.pluginOptions object is required. Diffusion will close the connection if it does not receive the originvalue.

document.addEventListener('deviceready', function () {
	WebSocket.pluginOptions = {
		origin: '',
		maxConnectTime: 5000,
		override: true

	var ws = new WebSocket('ws://<YOUR DIFFUSION IP>:8080/diffusion?v=4&ty=WB');

	ws.onopen = function () {

	ws.onmessage = function (event) {

	ws.onerror = function () {
		console.log('error occurred!');

	ws.onclose = function (event) {
		console.log('close code=' + event.code);
}, false);

Now that a WebSocket Connection has been created, it is now possible to create the connection to Diffusion. An example is shown below.

	disableFlash: true,
	disableSilverlight: true,
	debug: true,
	disableForeverFrame: true,
	disableXHR: true,
	wsURL: "ws://<YOUR DIFFUSION IP>:8080",
	wsTimeout: 6000,

	onDataFunction: function (msg) {
		console.log('MESSAGE RECEIVED');
	onCallbackFunction: function (isConnected) {
		console.log("Diffusion client is connected: " + isConnected);

	onConnectionRejectFunction: function () {

Native Android App

Currently there is not an external library compatible with Diffusion as they do not allow the origin header to be set. Android 4.4+ devices support WebSockets natively.