A Cordova plugin that uses OkHttp to provide WebSocket support in your Cordova app. It aims to mimic the WebSocket API in JavaScript, with additional features.
- ✅ WebSocket API-like interface
- ✅ Event support:
onopen,onmessage,onerror,onclose - ✅
extensionsandreadyStateproperties - ✅
listClients()to list active connections - ✅ Support for protocols
- ✅ Support for Custom Headers.
- ✅ Compatible with Cordova for Android
const WebSocketPlugin = cordova.websocket;WebSocketPlugin.connect("wss://example.com/socket", ["protocol1", "protocol2"], headers)
.then(ws => {
ws.onopen = (e) => console.log("Connected!", e);
ws.onmessage = (e) => console.log("Message:", e.data);
ws.onerror = (e) => console.error("Error:", e);
ws.onclose = (e) => console.log("Closed:", e);
ws.send("Hello from Cordova!");
ws.close();
})
.catch(err => console.error("WebSocket connection failed:", err));-
WebSocketPlugin.connect(url, protocols, headers, binaryType)- Connects to a WebSocket server.
url: The WebSocket server URL.protocols: (Optional) An array of subprotocol strings.headers: (Optional) Custom headers as key-value pairs.binaryType: (Optional) Initial binary type setting.- Returns: A Promise that resolves to a
WebSocketInstance.
-
WebSocketPlugin.listClients()- Lists all stored webSocket instance IDs.
- Returns:
Promisethat resolves to an array ofinstanceIdstrings.
-
WebSocketPlugin.send(instanceId, message, binary)- Sends a message to the server using an instance ID.
instanceId: The ID of the WebSocket instance.message: The message to send (string or ArrayBuffer/ArrayBufferView).binary: (Optional) Whether to send the message as binary, acceptsboolean- Returns:
Promisethat resolves when the message is sent.
-
WebSocketPlugin.close(instanceId, code, reason)- same as
WebSocketInstance.close(code, reason)but needsinstanceId. - Returns:
Promisethat resolves.
- same as
-
WebSocketInstance.send(message, binary)- Sends a message to the server.
message: The message to send (string or ArrayBuffer/ArrayBufferView).binary: (Optional) Whether to send the message as binary. acceptsboolean- Throws an error if the connection is not open.
-
WebSocketInstance.close(code, reason)- Closes the connection.
code: (Optional) If unspecified, a close code for the connection is automatically set: to 1000 for a normal closure, or otherwise to another standard value in the range 1001-1015 that indicates the actual reason the connection was closed.reason: A string providing a custom WebSocket connection close reason (a concise human-readable prose explanation for the closure). The value must be no longer than 123 bytes (encoded in UTF-8).
onopen: Event listener for connection open.onmessage: Event listener for messages received.onclose: Event listener for connection close.onerror: Event listener for errors.readyState: (number) The state of the connection.- 0 (
CONNECTING): Socket created, not yet open. - 1 (
OPEN): Connection is open and ready. - 2 (
CLOSING): Connection is closing. - 3 (
CLOSED): Connection is closed or couldn't be opened.
- 0 (
extensions: (string) Extensions negotiated by the server.binaryType: (string) Type of binary data to use ('arraybuffer' or '' (binary payload returned as strings.)).url: (string) The WebSocket server URL.instanceId: (string) Unique identifier for this WebSocket instance.
WebSocketInstance extends EventTarget, providing standard event handling methods:
addEventListener(type, listener): Registers an event listener.removeEventListener(type, listener): Removes an event listener.dispatchEvent(event): Dispatches an event to the object.
Example of using event listeners:
const ws = await WebSocketPlugin.connect("wss://example.com/socket");
// Using on* properties
ws.onmessage = (event) => console.log("Message:", event.data);
// Using addEventListener
ws.addEventListener('message', (event) => console.log("Message:", event.data));WebSocketInstance.CONNECTING: 0WebSocketInstance.OPEN: 1WebSocketInstance.CLOSING: 2WebSocketInstance.CLOSED: 3
- Only supported on Android (via OkHttp).
- Make sure to handle connection lifecycle properly (close sockets when done).
listClients()is useful for debugging and management.