This is a Cordova plugin, which is being developed based on Jetty 8, makes WebSocket (RFC6455) available on Android.
- Android 2.3 or later (recommended 4.1 or later)
email@example.com later, or compatible framework
The plugin for Cordova 2.x can be found here. However it is no longer maintained.
|version||WS protocol||WSS protocol||text message||binary message|
|2.3.x (API 10)||✓||✓||✓|
|4.0.x (API 14)||✓||✓||✓||✓|
|4.0.x (API 15)||✓||✓||✓||✓|
|4.1.x (API 16)||✓||✓||✓||✓|
|4.2.x (API 17)||✓||✓||✓||✓|
|4.3.x (API 18)||✓||✓||✓||✓|
|4.4.x and later||-||-||-||-|
- Since Android 4.4.x (KitKat) and later support WebSocket, this plugin is NOT used on there by default.
- WSS protocol is only supported TLS. SSL is not supported.
- Android 3.x (Honeycomb) are not supported (might work but is not tested).
firstname.lastname@example.org later, this plugin can be used together with Crosswalk. In this case also it is not used on there by default since that supports WebSocket.
- In order to support Android 5.x (Lollipop) and later, would be better to build with
Use Cordova Command-Line Interface (CLI). At first check your CLI version:
$ cordova --version5.0.0
If using 5.0.0 and later, you can install it via npm:
$ cordova plugin add cordova-plugin-websocket
If using other old versions, you can install it via GitHub:
$ cordova plugin add https://github.com/knowledgecode/WebSocket-for-Android.git
Setting a Content-Security-Policy (CSP)
email@example.com and later support SCP. In order to permit WebSocket access using
connect-src directive in
connect-src ws://example.com wss://example.com
Upgrading from previous versions
Remove and then reinstall:
$ cordova plugin rm cordova-plugin-websocket$ cordova plugin add cordova-plugin-websocket
- When install this plugin, it adds
platforms/android/AndroidManifest.xml. If remove this plugin, the permission is also removed at the same time even if it is required for other plugins.
- It has not supported
firstname.lastname@example.org earlier since v0.12.0. Please make sure Android platform version is more than that:
$ cordova platformInstalled platforms: android 4.1.1
The WebSocket(url, protocols) constructor takes one or two arguments. The first argument, url, specifies the URL to which to connect. The second, protocols, is either a string or an array of strings.
A simple code is as follows:
This plugin has the following options. All these parameters are optional. Of course these don't affect built-in WebSocket.
|key||type||default value||supported version|
|maxTextMessageSize||number||-1||>=v0.4.0 (except v0.8.x)|
|maxBinaryMessageSize||number||-1||>=v0.4.0 (except v0.8.x)|
|agent||string||(depends on device)||>=v0.9.0|
origin is a value to set a request header field. Default value is usually
file://. This is the same value as when using built-in WebSocket.
maxConnectTime is time to wait for connection. A unit is millisecond.
maxBinaryMessageSize are receivable maximum size from a server. Default value is -1 (unlimited. depends on heap size of devices). A unit is byte.
override is a flag to force WebView to use this plugin even if it supports WebSocket. However in most cases it will be slower than built-in WebSocket.
agent is user-agent to set a request header field. Default value depends on devices. This is the same value as when using built-in WebSocket.
perMessageDeflate is a flag whether to use permessage-deflate extension. Default value is true. Sends data with compression if a server also supports permessage-deflate. However if mainly sending compressed binary like JPEG images, recommended to set to false.
If change these parameters, need to do before creating a instance:
WebSocketpluginOptions =origin: ''maxConnectTime: 5000override: true;var ws = 'ws://echo.websocket.org';
Transmits data to the server over the WebSocket connection. The data takes a string, a blob, or an arraybuffer.
An upper limit of the message size depends on heap size of devices. It would be better to consider a way to split the message if it is quite large.
Closes the WebSocket connection or connection attempt, if any.
This plugin has been available logging for debug since v0.12.0. The logging level can be adjusted with
config.xml that is in your project directory:
If don't specify this parameter, default level is
DEBUG. So at first you may be surprised to see many debug logs on logcat. To stop logs except errors, change the level to
This plugin is available under the terms of the Apache License Version 2.0.