npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reconnecting-simple-websocket

v2.0.1

Published

WIP - nothing to see here

Downloads

23

Readme

reconnecting-simple-websocket

Actions Status

State machine for reconnecting simple-websockets. Subclass of reconnecting-socket.

npm install reconnecting-simple-websocket

Usage

const RSWS = require('reconnecting-simple-websocket')

const rsws = new RSWS('ws://localhost:8085', {
  onopen (socket, firstOpen) {
    // What gets called wheny you have a live socket
    console.log('onopen')
    this.pinger = setInterval(() => socket.write('ping'), 1000)
    socket.on('data', buf => console.log(buf.toString('utf8')))
  },

  onclose (socket) {
    // what to do when the socket is closed
    console.log('onclose')
    clearInterval(this.pinger)
  },

  onfail (err) {
    // handle the final error that caused the fail
    console.log('onfail')
    console.error(err)
  }
})

rsws.on('state', (st) => console.log(`state: ${st}`))
rsws.on('info', (st) => console.log(`info: ${st}`))
rsws.on('error', (err) => console.error(`error: ${err}`))

rsws.start() // start it

RSWS can also be subclassed:

const RSWS = require('reconnecting-simple-websocket')

class MySWS extends RSWS {
  constructor (opts) {
    super(opts)

    this.someHandler = this.someHandler.bind(this)
  }

  someHandler (buf) {
    // whatever
  }

  onopen (socket, firstOpen) {
    this.pinger = setInterval(() => socket.write('ping'), 1000)
    socket.on('data', this.someHandler)
  }

  onclose (socket) {
    socket.removeListener('data', this.someHandler)
    clearInterval(this.pinger)
  }

  onfail (err) {
    console.error(err)
  }
}

module.exports = MySWS

API

const RSWS = require('reconnecting-simple-websocket')

Import the RSWS class.

rsws = new ReconnectingSocket(opts)

Create a new reconnecting simple websocket instance. The opts object can receive the user implemented methods listed below as well as the following optional options:

{
  backoff: {
    strategy: 'fibonacci', // the backoff strategey to use
    failAfter: null, // the number of consecutive times to attemt a reconnect before failing
    ...backoffOptions // all the options from the backoff module
  }
}

See full set of backoff options here: MathieuTurcotte/node-backoff

rsws Events

An instance of rsws inherets the same events as reconnecting-socket.

  • state: The state emmits the following state strings:
    • stopped
    • opening
    • opened
    • closing
    • closed
    • reopening
    • fail
  • info: Messages that can be used for debugging.
  • error: Errors emitted by the socket or internally. For an error handling path, see reconnectingSocket.onfail(err).

rsws.start()

Start the reconnecting simple websocket.

rsws.stop()

Stop the reconnecting simple websocket.

User Implemented Methods

rsws.onopen(socket, firstOpen)

This method is called whenever a new socket is created and the this.open() method has been called, so the socket should be live and ready for data. It receives the socket as well as firstOpen boolean which indicates if this is the first successful connection since calling reconnectingSocket.start(). Interact with the socket in this function body.

rsws.onclose(socket)

This method is called when the this.close() method is called. Use this to clean up any event listeners or intervals used on the socket.

rsws.onfail(err)

This method is called when the reconnectignSocket fails to connect after failAfter concecutive attempts. It receives the last error emitted by the various moving parts.

See also

License

MIT