js-websocket-reconnect-client
TypeScript icon, indicating that this package has built-in type declarations

0.0.13 • Public • Published

WebSocket Client with automatically reconnect

This is light-weight JavaScript WebSocket library that supports reconnect

Installation

To install the stable version if you are using npm:

npm install js-websocket-reconnect-client

or if you are using yarn:

yarn add js-websocket-reconnect-client

Description

This module is wrapper around WebSocket that added few more functionalities.

The WebSocketClient object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

Usage

Import js-websocket-reconnect-client module if you are using ES modules:

import WebSocketClient from "js-websocket-reconnect-client";

This is simple example of js-websocket-reconnect-client usage:

import WebSocketClient from "js-websocket-reconnect-client";
 
const URL = "http://localhost:8000/ws/"; // set your url defined on server
 
const ws = new WebSocketClient(url);
 
ws.addOnMessageHandler(message => {
  console.log("[socket] message received", message);
});
 
ws.connect();

Creating object

This is definition for creating new object:

new WebSocketClient(
  urlstring,
  protocols?: string | string[],
  options?: {
    shouldReconnect: boolean;
    reconnectRetrynumber;
  }
);

Example:

import WebSocketClient from 'js-websocket-reconnect-client';
 
const url = "http://your.cool.domain/ws/";
const protocols = ["wamp", "soap"];
const options = {
  shouldReconnect: true;
  reconnectRetryTimeout2000;
  parsedMessagetrue;
  reconnectRetryMaxNumber10;
  debugfalse;
}
 
const webSocket = new WebSocketClient(url, protocols, options);

Url

URL is required parameter of type string. It specifies WebSocket connection endpoint. This should be the URL to which the WebSocket server will respond.

Example:

const url = "http://your.cool.domain/ws/";

Protocols

Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified protocol). If you don't specify a protocol string, an empty string is assumed.

Example:

const protocol = ["wamp", "soap"];

Options

Options are extended, custom part of this package. It is possible to specify several important features. If options object is not specified, a default values are is assumed.

Definition:

options{
  shouldReconnectboolean;
  reconnectRetryTimeoutnumber;
  parsedMessageboolean;
  reconnectRetryMaxNumber?: number;
  debug?: boolean;
}

Default:

const options = {
  shouldReconnect: true,
  reconnectRetryTimeout: 1000,
  parsedMessage: true,
  debug: false
};

Example:

const options = {
  shouldReconnect: true;
  reconnectRetryTimeout: 2000;
  parsedMessage: true;
  reconnectRetryMaxNumber: 10;
  debug: false;
}

Object Life Cycle Function

Readme

Keywords

none

Package Sidebar

Install

npm i js-websocket-reconnect-client

Weekly Downloads

1

Version

0.0.13

License

MIT

Unpacked Size

12.5 kB

Total Files

8

Last publish

Collaborators

  • pmaric