npm.io
0.0.16 • Published 2 years ago

resilient-ws

Licence
MIT
Version
0.0.16
Deps
0
Size
11 kB
Vulns
0
Weekly
0
Stars
1

npm version

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',
  },
})

Keywords