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

rapport-reconnect

v0.1.2

Published

Reconnect plugin for the Rapport request/response library

Downloads

25

Readme

rapport-reconnect CircleCI Coverage Status

NPM

A simple plugin that adds reconnect functionality to the Rapport library.

Installation

Node: Install the plugin via NPM: npm install --save rapport-reconnect

Browser: Attach rapport.reconnect.min.js to your HTML page

Then add the plugin to rapport:

// Globally
Rapport.use(require('rapport-reconnect')); // In Node.js
Rapport.use(RapportReconnect); // In the browser

// Or to a instance
Rapport(wsImplementation).use(require('rapport-reconnect')); // In Node.js
Rapport(wsImplementation).use(RapportReconnect); // In the browser

Basic Usage

This plugin adds reconnect capabilities to a socket. The reconnect functionality is specified in the rapport options object:

// Enable reconnect on all sockets created by the rapport library
Rapport.configure({ reconnect: true });

// Or all sockets created by a rapport instance:
const rapport = Rapport(wsImplementation, { reconnect: true }); // Or
const rapport = Rapport(wsImplementation).configure({ reconnect: true });

// Or a single socket
rapport.create('ws://hello.world', { reconnect: true });

NOTE: Only sockets that are created with .create() can have reconnect functionality. Specifying a reconnect value for a socket that is wrapped with .wrap() will do nothing.

Open and Error Handlers

When this plugin is added to Rapport, the onOpen handler will be called every time a new connection is established, with an object parameter:

const ws = rapport.create('ws://hello.world', { reconnect: true });

ws.onOpen((openData) => {
     console.log(`Successfully established connection after ${openData.retryAttempts} reconnect attempts`);
     console.log(`Total number of socket opens: ${openData.openCount}`);
     console.log(`Total number of socket closes: ${openData.closeCount}`);
     
     if (openData.reconnect) {
         console.log('Socket has been reconnected');
     } else {
         console.log('Socket has been opened for the first time');
     }
});

Additionally, every time the connection is dropped, the onError handler will be called:

const ws = rapport.create('ws://hello.world', { reconnect: true });

ws.onError((err) => {
    if (err.name === 'RapportReconnect') {
        console.log(`Socket was closed with a code of ${err.code} and message of ${err.message}`);
        console.log(`Attempting connection again after ${err.retryAttempts} attempts`);
        console.log(`Total number of socket closes: ${err.closeCount}`);
        console.log(`Total number of socket opens: ${err.openCount}`);
    }
});

Reconnect Options

By default, reconnection will be attempted on an interval of 500 milliseconds, with no stop condition. All of the following calls are equivalent:

rapport.create('url', { reconnect: true });
rapport.create('url', { reconnect: 'interval' });
rapport.create('url', { reconnect: { type: 'interval' } });
rapport.create('url', { reconnect: { type: 'interval', interval: 500 } });
rapport.create('url', { reconnect: { type: 'interval', interval: 500, maxAttempts: 0 } });

You can change the maxAttempts and interval as you see fit. When retrying fails (because the maxAttempts have been exceeded), the onClose handler will be called as normal.

More retry methods (fibonacci and exponential) will be implemented in future versions. If there is another reconnect method you would like to see, please open an issue describing it.

Message Queueing

The reconnect plugin also supports message queueing. When enabled, messages that are sent during a reconnect will be pushed onto a queue. When the connection is established, they will all be sent immediately. The following options all enable simple message queueing (and the default interval reconnect shown above):

rapport.create('url', { reconnect: { queueMessages: true } });
rapport.create('url', { reconnect: { queueMessages: { type: 'simple' } } });

More queueing mechanisms can be implemented as needed, please open an issue with any requests.