1.3.3 • Published 8 months ago

@unless.com/wsmodule v1.3.3

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

WSModule

WSModule is a utility module designed for managing WebSocket (WS) connections. It's a robust module that includes features like automatic reconnection with exponential backoff, ping handling, and activity-based connection handling.

Features:

  • Automatic reconnection on connection loss with exponential backoff.
  • Heartbeat via the ping mechanism to ensure connection validity.
  • Updates the WebSocket connection with specific identifiers.
  • Tracks idle state to optimize resource usage.
  • Streamlined error handling and callbacks.

Installation

npm -i @unless.com/wsmodule # npm

yarn add @unless.com/wsmodule # yarn

pnpm add @unless.com/wsmodule # pnpm

Importing:

import { WSModule } from "@unless.com/wsmodule";

Usage:

  1. Initialization:
const wsModule = new WSModule({
  handleMessageCallback: yourMessageCallbackFunction,
  handleWSErrorCallback: yourErrorCallbackFunction,
  endpoint: new URL("wss://your-websocket-url.com"),
});
  1. Connecting to WebSocket:
await wsModule.connect();
  1. Disconnection from WebSocket:
wsModule.disconnect();
  1. Sending a ping:
wsModule.ping();
  1. Updating the connection with specific identifiers:
wsModule.updateConnection({
  visitorId: "someId",
  accountId: "accountId",
  websiteId: "websiteId",
  visitId: "visitId",
});
  1. Handling Activity (useful to reset idle timeouts and possibly re-establish a ping cycle):
wsModule.onActivity();

Configuration:

You can configure the following properties when initializing WSModule:

  • handleMessageCallback: A function that handles incoming WebSocket messages.
  • handleWSErrorCallback: A function called when there's an error with the WebSocket.
  • endpoint: The WebSocket URL you want to connect to.
  • maxConnectionRetries (optional): Maximum number of reconnection attempts.
  • initialConnectionRetryDelay (optional): Initial delay before trying to reconnect.
  • pingTimeout (optional): Timeout for the ping operation.
  • pingInterval (optional): Interval to send pings.
  • idleDuration (optional): Duration to determine idleness.
1.3.3

8 months ago

1.3.2

9 months ago

1.3.0

9 months ago

1.2.5

9 months ago

1.2.4

9 months ago

1.2.3

9 months ago

1.2.2

9 months ago

1.2.1

9 months ago

1.2.0

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.13

9 months ago

1.0.12

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago