- automatic retries when the message cannot be sent (e.g. when the connection is lost)
- ping/pong to keep the connection alive
npm install resilient-ws
OR
yarn add resilient-ws
OR
pnpm add resilient-ws
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,
})
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',
},
})