resilient-ws
v0.0.16
Published
A resilient websocket client for Node.js and the browser. Provides automatic reconnection, message retries, ping/pong & message queueing.
Downloads
32
Maintainers
Readme
Resilient Websockets
- automatic retries when the message cannot be sent (e.g. when the connection is lost)
- ping/pong to keep the connection alive
Installation
npm install resilient-ws
OR
yarn add resilient-ws
OR
pnpm add resilient-ws
Usage
import { ResilientWS } from 'resilient-ws'
ResilientWS.create({
url: '',
onConnectCallback: () => {
console.log(`Wow this is a websocket connection!`)
},
onDisconnectCallback: () => {
console.log(`Wow this is a websocket disconnection!`)
},
onMessageCallback: (message) => {
console.log(`Wow this is a websocket message!`, message)
},
onErrorCallback: (error) => {
console.log(`Wow this is a websocket error!`, error)
},
})
ResilientWS maintains a single instance. So you can import it anywhere and use it.
import { ResilientWS } from 'resilient-ws'
const ws = ResilientWS.getInstance()
ws.send({
message: 'Hello World!'
attempt: 0,
forceReconnect: false,
})
Ping/Pong
By default, ping/pong is disabled. You can enable it by passing the config for ping/pong when creating your ResilientWS instance.
import { ResilientWS } from 'resilient-ws'
ResilientWS.create({
...config,
pingPongSettings: {
enabled: true
pingInterval: 10000,
pingMessage: 'ping',
},
})