WIP - nothing to see here
Last updated a year ago by bret .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install reconnecting-simple-websocket 
SYNC missed versions from official npm registry.


Actions Status

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

npm install reconnecting-simple-websocket


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
    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

  onfail (err) {
    // handle the final error that caused the fail

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) {

    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)

  onfail (err) {

module.exports = MySWS


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).


Start the reconnecting simple websocket.


Stop the reconnecting simple websocket.

User Implemented Methods

rsws.onopen(socket, firstOpen)

This method is called whenever a new socket is created and the 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.


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.


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



Current Tags

  • 2.0.1                                ...           latest (a year ago)

4 Versions

  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
Maintainers (2)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (8)
Dependents (0)

Copyright 2014 - 2017 © |