Websocket-heartbeat-miniprogram (v1.0.3) is a lightweight WebSocket heartbeat library designed for WeChat Mini Programs and compatible platforms (Alipay, Baidu, Uni-app, Taro). It wraps the mini-program's WebSocket API with automatic heartbeat detection and reconnection logic, ensuring stable long-lived connections in unreliable network environments. Key differentiator: it handles platform-specific quirks (e.g., Alipay's single-socket limit) and preserves custom event hooks across reconnections, unlike the native socketTask that loses listeners after reconnect. The library is ESM-only, returns a promise, and provides a task object with stable hooks (onOpen, onClose, onMessage, onError, onReconnect) as well as the raw socketTask. Release cadence appears low; last release was a while ago. The package has minimal dependencies and is focused solely on mini-program environments.
npm install websocket-heartbeat-miniprogramNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows full setup: import, call WebsocketHeartbeat with platform (wx) and connection params, then attach all stable hooks (onOpen, onClose, onMessage, onError, onReconnect) to the returned task object. Also demonstrates raw socketTask usage and error handling.
Use import syntax (import WebsocketHeartbeat from 'websocket-heartbeat-miniprogram') or transpile with Babel/esbuild. Do NOT use require().
Always call `task.close()` on the old task before starting a new one. The library does not handle this automatically for Alipay.
Server should send a close message instead. Frontend checks that message and calls `task.close()` to stop reconnection.
Use task.onOpen, task.onMessage, etc. instead of socketTask.onOpen, etc., to keep listeners across reconnects.
Implement your own token refresh logic inside a custom function and call it before initiating a new connection, possibly overriding the default reconnect behavior.
Change to import syntax: `import WebsocketHeartbeat from 'websocket-heartbeat-miniprogram'`
Ensure you pass the correct mini-program API object: `wx` for WeChat, `tt` for ByteDance, `swan` for Baidu, `my` for Alipay, or `uni` for Uni-app.
Provide a valid WebSocket URL in `connectSocketParams.url`. Example: `{ url: 'wss://example.com/socket' }`For server close: have server send a close message, frontend checks and calls `task.close()`. For Alipay conflicts: ensure only one task exists at a time by calling `task.close()` on the old task before creating a new one.
No dependency data recorded yet.