websocket-pool
v1.3.2
Published
A pool of WebSocket connections. Supports reconnecting.
Downloads
37
Maintainers
Readme
websocket-pool
A pool of WebSocket connections. Supports reconnecting. Allows load-balancing messages over multiple WebSocket servers, using any scheduling scheme (e.g. round robin).
Installation
npm install websocket-pool
Usage
const createPool = require('websocket-pool')
const WebSocket = require('ws')
const createRoundRobin = require('@derhuerst/round-robin-scheduler')
const pool = createPool(WebSocket, createRoundRobin)
// incoming message, just like websocket.on('message')
pool.on('message', (msg) => {
console.log('<-', msg.data)
pool.close()
})
// >= 1 connection in the pool is open
pool.once('open', () => {
pool.send('hello there')
})
// the pool failed to reconnect after retrying
pool.on('error', (err) => {
console.error(err)
})
const urls = [
'ws://echo.websocket.org/#1',
'ws://echo.websocket.org/#2',
'ws://echo.websocket.org/#3'
]
for (let url of urls) pool.add(url)
websocket-pool
accepts any WebSocket
implementation. For example, you can use the native implementation in browsers or ws
in Node.
The createScheduler
function must implement the abstract-scheduler
interface.
Related
reconnecting-websocket
– If want to connect to only one server.node-pool
– Generic pool. You have to write the adapter to the resource.
Contributing
If you have a question or need support using websocket-pool
, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, refer to the issues page.