Registry / messaging / cordova-plugin-websocket-server

cordova-plugin-websocket-server

JSON →
library1.6.0jsnpmunverified

A Cordova plugin that enables running a single, lightweight WebSocket server on Android and iOS devices. Version 1.6.0 is the latest release. It allows starting/stopping a server on a specified port, managing WebSocket connections (open, message, close events), and sending/receiving text and binary messages. The server is not a background service and stops when the Cordova view is destroyed. Key differentiators: simple callback-based API, built-in support for network interface enumeration, and origin/protocol validation. The plugin relies on platform-specific libraries: TooTallNate Java-WebSocket for Android and PocketSocket for iOS.

npm install cordova-plugin-websocket-server
INSTALL
IMPORT
SIG · CORDOVA-PLUGIN-WEB
C
cordova-plugin-websocket-server
messagingjavascriptv1.6.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

wsserver
✓ var wsserver = cordova.plugins.wsserver;
✗ import wsserver from 'cordova-plugin-websocket-server';
This is a Cordova plugin, so it must be accessed via cordova.plugins after device ready. ES6 import does not work.
start
✓ wsserver.start(port, options, successCallback, failureCallback);
The start method takes a port number, an options object with event handlers, and success/failure callbacks.
stop
✓ wsserver.stop(successCallback, failureCallback);
Stops the server. The success callback receives the address and port that were stopped.
send
✓ wsserver.send(conn, msg);
✗ wsserver.send(uuid, msg);
The first parameter must be an object with a 'uuid' property, not just the UUID string.

Demonstrates starting a WebSocket server on port 8080 after device ready, with event handlers for connection lifecycle.

document.addEventListener('deviceready', function () { var wsserver = cordova.plugins.wsserver; wsserver.start(8080, { onFailure: function(addr, port, reason) { console.log('Failure: ' + reason); }, onOpen: function(conn) { console.log('Connected: ' + conn.remoteAddr); }, onMessage: function(conn, msg) { console.log('Message: ' + msg); }, onClose: function(conn, code, reason, wasClean) { console.log('Disconnected'); } }, function(addr, port) { console.log('Server started on ' + addr + ':' + port); }, function(reason) { console.log('Failed to start: ' + reason); }); }, false);
Debug
Known issues
gotchaServer stops when Cordova view is destroyed. Not a background service.
fix
Do not rely on the server persisting after app is backgrounded or terminated.
affects: >=1.0.0
gotchaFirst parameter to send() must be an object with uuid, not just a UUID string.
fix
Use wsserver.send({uuid: '...'}, msg) instead of wsserver.send('...', msg).
affects: >=1.0.0
gotchawsserver.start port 0 does not bind to a random port; it binds to port 0 which is invalid on some platforms.
fix
Pass a specific port number (e.g., 8080) instead of 0.
affects: >=1.0.0
deprecatedThe success and failure callbacks in start() are optional but if omitted, errors are silent.
fix
Always provide at least a failure callback to catch errors.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: cordova is not defined
Code executed before device ready event or outside Cordova environment.
fix
Wrap code in 'deviceready' event listener or run on a real device/emulator.
TypeError: wsserver.send is not a function
Plugin not loaded or wsserver accessed incorrectly.
fix
Ensure cordova.plugins.wsserver is available after deviceready. Check plugin installation.
Error: start failed (websocket server could not bind to port)
Port already in use or network permission missing.
fix
Choose an unused port (e.g., >1024) and verify network permissions in config.xml.
Upgrade
Version history
1.6.0latest on npm
Audit
Dependencies
cordovarequiredPlugin framework required for installation and usage
Agent activity
41 hits · last 30 days
node
36
OpenAI (training)
1
Resources
cordova-plugin-websocket-server — npm install cordova-plugin-websocket-server · libregistry