resilient-ws
TypeScript icon, indicating that this package has built-in type declarations

0.0.16 • Public • Published

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

Package Sidebar

Install

npm i resilient-ws

Weekly Downloads

12

Version

0.0.16

License

MIT

Unpacked Size

11.4 kB

Total Files

8

Last publish

Collaborators

  • bilalafzal01