Skip to content

Commit

Permalink
simplify the api; update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Jan 21, 2024
1 parent 2d667fc commit 31d136c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import WSReqonet from "ws-reqonet";
const url = `ws://localhost:3001/`;
const protocols = [];
const options = {
maxReconnectAttempts: 5,
maxRetryAttempts: 3,
maxRetryAttempts: 10,
queueMessage: true,
};

Expand Down Expand Up @@ -81,41 +80,34 @@ Either a single protocol string or an array of protocol strings. These strings a

Type: `object`

#### maxReconnectAttempts

Type: `number`\
Default: 5

Number of times it attempts to reconnect within a retry

#### maxRetryAttempts

The maximum number of retries - how many attempts at reconnecting

Type: `number`\
Default: 5

The maximum number of retries - how many attempts at reconnecting

#### queueMessage

Whether to store 'send' data when the connection is broken, which is to be relayed when connection is restored.

Type: `boolean`\
Default: true

Whether to store 'send' data when the connection is broken, which is to be relayed when connection is restored.

#### disableReconnect

Whether to disable reconnection

Type: `boolean`\
Default: false

Whether to disable reconnection

#### debug

Whether to run in debug mode which enables logging to dev tools

Type: `boolean`\
Default: false

Whether to run in debug mode which enables logging to dev tools

### events

#### open, close, message, error
Expand Down
13 changes: 5 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { EventEmitter } from "events";

const MAX_RECONNECT_ATTEMPTS = 10;
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
export interface WSReqonetOptions {
/** # of times to reconnect within a retry */
maxReconnectAttempts?: number;
/** # of attempts at reconnecting */
/** # attempts to reconnect */
maxRetryAttempts?: number;
/** Whether to store 'send' data when connection is broken */
queueMessage?: boolean;
Expand All @@ -16,14 +15,13 @@ export interface WSReqonetOptions {
// websocket with reconnection on exponential back-off
export default class WSReqonet extends EventEmitter {
private ws: WebSocket;
private reconnectAttempts = 0;
private maxReconnectAttempts: number;
private retryAttempts = 0;
private maxRetryAttempts: number;
private queueMessage: boolean;
private messageQueue: Array<string | Blob | ArrayBuffer | ArrayBufferView> =
[];

private reconnectAttempts = 0;
private intervalRef = 0;
private forcedClose = false;
private disableReconnect: boolean;
Expand All @@ -35,8 +33,7 @@ export default class WSReqonet extends EventEmitter {
) {
super();

this.maxReconnectAttempts = options.maxReconnectAttempts ?? 5;
this.maxRetryAttempts = options.maxRetryAttempts ?? 5;
this.maxRetryAttempts = options.maxRetryAttempts ?? 10;
this.queueMessage = options.queueMessage ?? true;
this.disableReconnect = options.disableReconnect ?? false;

Expand Down Expand Up @@ -106,7 +103,7 @@ export default class WSReqonet extends EventEmitter {

const TIMEOUT = Math.pow(2, this.retryAttempts + 1) * 1000;
const reconnectHandler = () => {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
if (this.reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
this.reconnectAttempts++;
console.log("ws: reconnecting - attempt: ", this.reconnectAttempts);

Expand Down

0 comments on commit 31d136c

Please sign in to comment.