Registry / messaging / wxapp-websocket

wxapp-websocket

JSON →
library1.1.10jsnpmunverified

A WebSocket library for WeChat Mini Programs. Current stable version is 1.1.10, released with support for single and multiple WebSocket connections based on the base library version. It wraps the native wx.connectSocket API and provides a Promise-based interface with automatic reconnection and event handling. It simplifies WebSocket communication in WeChat Mini Programs, which have unique constraints compared to browser environments.

npm install wxapp-websocket
INSTALL
IMPORT
SIG · WXAPP-WEBSOCKET
W
wxapp-websocket
messagingjavascriptv1.1.10
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 Websocket from 'wxapp-websocket'
const Websocket = require('wxapp-websocket')
The package exposes a default export; ESM import is recommended for module systems supporting ES modules. In WeChat Mini Programs, use the module system provided by the framework.
Websocket
import Websocket from 'wxapp-websocket'
import { Websocket } from 'wxapp-websocket'
The library provides a default export, not a named export. Incorrect named imports will result in undefined.
single connection mode
const ws = new Websocket('wss://example.com')
const ws = new Websocket({ url: 'wss://example.com' })
In single connection mode, the constructor expects a URL string, not an options object. This differs from multi-connection mode.
multi-connection mode
const ws = new Websocket.Multi('wss://example.com')
const ws = new Websocket('wss://example.com', { multi: true })
For multiple connections, use the static Multi constructor. Passing options to the default constructor will not enable multi-connection.

Demonstrates importing the module, creating a single WebSocket connection, and using event handlers. Also shows multi-connection mode with the Multi constructor.

import Websocket from 'wxapp-websocket'; // Single connection mode const ws = new Websocket('wss://example.com'); ws.onopen = () => { console.log('WebSocket opened'); }; ws.onmessage = (event) => { console.log('Received:', event.data); }; ws.onerror = (error) => { console.error('WebSocket error:', error); }; ws.onclose = () => { console.log('WebSocket closed'); }; // Multi-connection mode const MultiWs = Websocket.Multi; const ws1 = new MultiWs('wss://example1.com'); const ws2 = new MultiWs('wss://example2.com'); // Close connection ws.close();
Debug
Known issues
gotchaThe default constructor uses single connection mode, which may automatically close existing connections in older base libraries (< 1.7.0).
fix
Use Multi constructor for multiple connections regardless of base library version.
affects: all
deprecatedThe API 'wx.connectSocket' used internally has deprecated callback methods; events should be handled via SocketTask.
fix
Ensure you are using event handlers like onopen, onmessage, onerror, onclose as shown in examples.
affects: >=1.1.0
gotchaIn single connection mode, calling 'new Websocket(url)' with an object like {url: '...'} will fail silently or create an undefined connection.
fix
Pass a string URL to the constructor in single mode; use Multi constructor for object parameters.
affects: all
gotchaThe library does not support browser WebSocket; it is only for WeChat Mini Program environment.
fix
Do not use this library outside of WeChat Mini Program development.
affects: all
Errors
Common errors & fixes
Websocket is not defined
Incorrect import syntax or module system not supported.
fix
Use default import: import Websocket from 'wxapp-websocket'.
Cannot read property 'onopen' of undefined
Tried to access event handlers on a non-instantiated object, possibly due to wrong constructor usage.
fix
Ensure constructor is called with correct arguments (string URL for single mode) and that the module is properly imported.
wx.connectSocket is not a function
The code is running outside of a WeChat Mini Program environment (e.g., in a browser or Node.js).
fix
Only use this library within WeChat Mini Program development.
Upgrade
Version history
1.1.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
28 hits · last 30 days
node
24
OpenAI (training)
1
Resources
wxapp-websocket — npm install wxapp-websocket · libregistry