Registry / networking / websocket-pool

websocket-pool

JSON →
library1.3.2jsnpmunverified

A pool of WebSocket connections with automatic reconnection and load balancing. Version 1.3.2 is the current stable release, with no new releases since 2019. It allows distributing messages across multiple WebSocket servers using a pluggable scheduler (e.g., round-robin). Supports any WebSocket implementation (native or `ws`). Different from `reconnecting-websocket` which handles a single connection, and `generic-pool` which requires custom adaptation. Node.js >=6. ISC license.

npm install websocket-pool
INSTALL
IMPORT
SIG · WEBSOCKET-POOL
W
websocket-pool
networkingjavascriptv1.3.2
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.

createPool
import createPool from 'websocket-pool'
const createPool = require('websocket-pool')
Package uses CommonJS `module.exports`, so default import works both with ESM and CJS. However, TypeScript users may need `const createPool = require('websocket-pool')` if `esModuleInterop` is off.
Pool
type Pool = ReturnType<typeof createPool>
No explicit type export. Use TypeScript inference or declare your own type.
events
import { EventEmitter } from 'events'; const pool = createPool(...); pool.on('message', ...)
Pool inherits from EventEmitter. No need to import any additional symbols.

Creates a WebSocket pool with round-robin scheduling and two connections, sends a message on open, logs incoming messages, and closes the pool.

const createPool = require('websocket-pool'); const WebSocket = require('ws'); const createRoundRobin = require('@derhuerst/round-robin-scheduler'); const pool = createPool(WebSocket, createRoundRobin); pool.on('open', () => { pool.send('hello'); }); pool.on('message', (msg) => { console.log('received:', msg.data); pool.close(); }); pool.on('error', (err) => { console.error(err); }); ['ws://echo.websocket.org/#1', 'ws://echo.websocket.org/#2'].forEach(url => pool.add(url));
Debug
Known issues
gotchaThe pool does not emit 'open' when the first connection opens; it emits 'open' when at least one connection is open.
fix
Bind to 'open' event after adding URLs.
affects: >=1.0.0
gotchaThe scheduler function must implement the abstract-scheduler interface exactly. Using a non-compliant scheduler may cause silent failures.
fix
Check the scheduler implementation against https://github.com/derhuerst/abstract-scheduler.
affects: >=1.0.0
breakingVersion 1.0.0 changed the API: the first argument to createPool is now the WebSocket constructor (was the constructor's prototype).
fix
Update to use `createPool(WebSocket, scheduler)` instead of `createPool(WebSocket.prototype, scheduler)`.
affects: <1.0.0
gotchaThe pool does not support per-connection options like headers or subprotocols. All connections are created with the same options.
fix
If you need per-connection customization, consider using a different library or forking.
affects: >=1.0.0
deprecatedThe `ws` package version 7.x has breaking changes in WebSocket constructor signature. This pool may not be compatible with newer `ws` versions.
fix
Use `ws@6` or test compatibility with your version.
affects: >=7.0.0
Errors
Common errors & fixes
TypeError: createPool is not a function
Using default import with TypeScript or bundler that does not handle CJS default export correctly.
fix
Use `const createPool = require('websocket-pool');` or enable `esModuleInterop` in tsconfig.
Error: Invalid scheduler. Expected a function.
The second argument to createPool is not a function or does not conform to the scheduler interface.
fix
Pass a valid scheduler factory, e.g., `createRoundRobin` from '@derhuerst/round-robin-scheduler'.
TypeError: pool.on is not a function
Trying to use pool methods before pool is fully initialized, or pool is undefined due to incorrect import.
fix
Ensure `createPool` returned a valid pool object. Check import syntax.
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies
@derhuerst/round-robin-scheduleroptionalRequired to use round-robin scheduling (the default scheduler example).
Agent activity
22 hits · last 30 days
node
18
Amazon
1
OpenAI (training)
1
Resources
websocket-pool — npm install websocket-pool · libregistry