rapport-reconnect

0.1.2 • Public • Published

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.

Package Sidebar

Install

npm i rapport-reconnect

Weekly Downloads

0

Version

0.1.2

License

MIT

Unpacked Size

21.9 kB

Total Files

10

Last publish

Collaborators

  • miratronix