ws-client-js
v1.0.5
Published
WebSocket browser client implementation with reconnect behavior.
Downloads
7
Readme
ws-client-js
WebSocket client implementation with reconnect behavior.
Install
yarn add ws-client-js
or
npm install ws-client-js --save
Usage
import { Connection, JsonSerializer, CLOSE_EVENT_CODE } from 'ws-client-js';
// JSON serializer for send and receive message
const serializer = new JsonSerializer();
const connection = new Connection(serializer, {
// websocket endpoint
url: 'ws://localhost:8080',
reconnect: {
// 1 seconds
delay: 1000,
// 'default' | 'twice'
// After each attempt, the delay increases based on the type. 'default' does not increase
delayIncreaseType: 'twice',
// number of attempts
attempts: 5,
// connection closure codes for which reconnect will not work
skipCloseEventCodes: [CLOSE_EVENT_CODE.NORMAL, CLOSE_EVENT_CODE.NO_STATUS_RESERVED],
},
debug: false, // log debug messages
});
// use for start connection to endpoint
connection.connect();
// use for start disconnect to endpoint
connection.disconnect();
Api methods
- Start connection process
connect(protocol?: string | string[]): void;
- Start disconnect process
disconnect(closeEvent?: TCloseEvent): void;
- Send message to server. The message will be serialized using ISerialize implementation
send<T = unknown>(data: T): boolean;
- Call callback when the message arrives. The message will be deserialized using ISerialize implementation
onMessage(cb: () => void): TDisposer;
- Call callback when connection open
onOpen(cb: () => void): TDisposer;
- Call callback when connection open after reconnect
onReopen(cb: () => void): TDisposer;
- Call callback when a connection error occurs
onError(cb: () => void): TDisposer;
- Call callback when the connection closes
onClose(cb: (event: TCloseEvent) => void): TDisposer;