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:
;
This is simple example of js-websocket-reconnect-client usage:
; ; // set your url defined on server ; ws.addOnMessageHandler; ws.connect;
Creating object
This is definition for creating new object:
new WebSocketClient url: string, protocols?: string | string, options?: ;
Example:
; ;; ;
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:
;
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:
;
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:
Default:
const options = shouldReconnect: true reconnectRetryTimeout: 1000 parsedMessage: true debug: false;
Example:
const options = shouldReconnect: true; reconnectRetryTimeout: 2000; parsedMessage: true; reconnectRetryMaxNumber: 10; debug: false;