Registry / messaging / plus-websocket

plus-websocket

JSON →
library1.0.7jsnpmunverified

A library that provides mini-program-style WebSocket APIs for HTML5+ (5+ App), web browsers, and uni-app environments (excluding mini-programs). Current stable version 1.0.7 is released irregularly. It solves ArrayBuffer type data limitation and multiple WebSocket connection issues in uni-app, and prevents white screen on some Android devices. Differentiates by offering the same API as uni-app's WebSocket but with broader platform support and bug fixes.

npm install plus-websocket
INSTALL
IMPORT
SIG · PLUS-WEBSOCKET
P
plus-websocket
messagingjavascriptv1.0.7
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.

default
import socket from 'plus-websocket'
import { socket } from 'plus-websocket'
Package exports a default object, not named export.
connectSocket
import socket from 'plus-websocket'; socket.connectSocket({...})
const { connectSocket } = require('plus-websocket')
All APIs are properties of the default export. CommonJS destructuring will not work.
SocketTask
import socket from 'plus-websocket'; const task = socket.connectSocket({...})
import { SocketTask } from 'plus-websocket'
SocketTask is not exported directly; it's the return type of connectSocket.

Connects to a WebSocket server, sends a message, listens for incoming messages, and closes after 5 seconds.

import socket from 'plus-websocket'; import { WebSocket } from 'ws'; // polyfill for Node.js if needed // Connect to WebSocket server const task = socket.connectSocket({ url: 'wss://echo.websocket.org', success() { console.log('connected'); // Send a message task.send({ data: 'Hello' }); }, fail(err) { console.error('connection failed', err); } }); // Listen for messages task.onMessage((res) => { console.log('received:', res.data); }); // Close after 5 seconds setTimeout(() => { task.close({ code: 1000, reason: 'bye' }); }, 5000);
Debug
Known issues
gotchaThis library is not designed for mini-program environments (WeChat, Alipay, etc.). Use the native uni-app WebSocket APIs instead.
fix
For mini-programs, directly use uni.connectSocket et al.
affects: >=0.0.0
gotchaThe default export's methods like connectSocket do not return a Promise; they use callback-based success/fail/complete.
fix
Wrap with Promise or use uni-app style callbacks.
affects: >=0.0.0
gotchaWhen using in uni-app's HBuilderX, ensure the import is placed inside a conditional compilation block (#ifdef APP-PLUS) to avoid conflicts with uni's built-in WebSocket API.
fix
Use conditional compilation: // #ifdef APP-PLUS
affects: >=0.0.0
gotchaThe package does not ship TypeScript definitions. You may need to declare module for TypeScript projects.
fix
Create a .d.ts file: declare module 'plus-websocket' { ... }
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'connectSocket' of 'webpack_require(...)' as it is undefined.
Attempted to use named import like import { connectSocket } from 'plus-websocket'.
fix
Use default import: import socket from 'plus-websocket'.
Uncaught ReferenceError: plus is not defined
Library depends on HTML5+ runtime (plus) which is only available in 5+ App environment.
fix
Ensure code runs in a 5+ App webview, or polyfill 'plus' object in browser for testing.
WebSocket connection to 'wss://...' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
Server is not running or incorrect URL provided.
fix
Verify the WebSocket server URL and ensure it is reachable from the client.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
plus-websocket — npm install plus-websocket · libregistry