Registry / communication / pws
library7.0.0jsnpmunverified

PWS (v7.0.0) is a drop-in replacement for the browser WebSocket API that adds automatic reconnection with exponential backoff (max 5 min) and configurable ping timeout to detect half-open connections. It works in both browsers and Node.js (requires the 'ws' library). Unlike alternatives like reconnecting-websocket, PWS closely mimics the standard WebSocket interface and triggers reconnect on browser 'online' events. Released by porsager with regular updates.

npm install pws
INSTALL
IMPORT
SIG · PWS
P
pws
communicationjavascriptv7.0.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.

PersistentWebSocket
import { PersistentWebSocket } from 'pws'
const PersistentWebSocket = require('pws').PersistentWebSocket
Default export is the PersistentWebSocket class. In CommonJS, use require('pws') directly.
default (class)
import PWS from 'pws'
const pws = require('pws').default
PWS is a default export. In CommonJS, require('pws') returns the class directly.
PWS (factory function for Node)
const PWS = require('pws'); const pws = PWS(url, WebSocket);
const pws = new PWS(url, WebSocket);
In Node.js, PWS is a factory function that takes a WebSocket constructor as the second argument. Do not use 'new'.

Shows browser usage with PersistentWebSocket class, including onopen, onmessage, and onerror events.

import { PersistentWebSocket } from 'pws'; const url = process.env.WS_URL ?? 'wss://echo.websocket.org'; const ws = new PersistentWebSocket(url, { pingTimeout: 30000, }); ws.onopen = () => { console.log('Connected'); ws.send('Hello'); }; ws.onmessage = (event) => { console.log('Received:', event.data); }; ws.onerror = (error) => { console.error('Error:', error); };
Debug
Known issues
breakingIn Node.js, PWS is a factory function, not a constructor. Using 'new' will throw an error.
fix
Use PWS(url, WebSocket) without 'new'.
affects: >=1.0.0
gotchaThe default export is the class itself, not a named export. In TypeScript, only 'PersistentWebSocket' is exported as named.
fix
Use import PWS from 'pws' or import { PersistentWebSocket } from 'pws'.
affects: >=7.0.0
gotchaThe options object (url, WebSocket) is specified as second argument in the constructor, not as an options bag in the first argument.
fix
Pass options as second argument: new PersistentWebSocket(url, { pingTimeout: 30000 }).
affects: >=1.0.0
deprecatedThe 'pws' package has been deprecated in favor of 'pws' v7? Check changelog. Some older versions may have different APIs.
fix
Upgrade to latest version and review API changes.
affects: <7.0.0
Errors
Common errors & fixes
TypeError: PWS is not a constructor
Using 'new PWS(url, WebSocket)' in Node.js, but PWS is a factory function.
fix
Remove 'new': const ws = PWS(url, WebSocket);
Cannot find module 'pws'
Missing npm install or typo in import.
fix
Run 'npm install pws' or check spelling.
Uncaught ReferenceError: PersistentWebSocket is not defined
Trying to use PersistentWebSocket without importing it.
fix
Add import: import { PersistentWebSocket } from 'pws';
TypeError: WebSocket is not a constructor
In Node.js, the 'ws' package is not installed or not passed correctly.
fix
Install 'ws' package: npm install ws, then require it and pass as second argument to PWS factory.
Upgrade
Version history
7.0.0latest on npm
Audit
Dependencies
wsoptionalRequired for Node.js usage; PWS expects a compatible WebSocket implementation.
Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
1
Resources
packagepws
pws — npm install pws · libregistry