client-websockets
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Client websockets

Introduction

This npm package provides easy-to-use WebSocket implementations for various scenarios, including polling-based, reconnecting, and Web3 subscriptions. The package is written in TypeScript and supports a generic data type for flexibility.

🩷 (please consider adding a ⭐ on github) 🩷

Installation

npm install client-websockets

Usage

Reconnecting WebSocket

import { ReconnectingWebSocket } from "client-websockets";

// Example usage
const reconnectingWebSocket = new ReconnectingWebSocket<string>({
  url: "wss://example.com",
  connectionData: { key: "value" },
  reconnectInterval: 1000,
  maxRetries: 10,
  nameIdentifier: "example",
  pingIntervalMs: 5000,
});

reconnectingWebSocket.connect(); // Connect the websocket
reconnectingWebSocket.disconnect(); // Disconnect the websocket
reconnectingWebSocket.onMessage((data) => {
  // Handle incoming messages
});

Polling WebSocket

import { PollingWebsocket } from "client-websockets";

// Example usage
const pollingWebSocket = new PollingWebsocket<string>({
  nameIdentifier: "example",
  pollingInterval: 3000,
  pollingFunction: () => {
    // fetch data and check if it has changed
    pollingWebSocket.send("Fetched data");
  },
});

pollingWebSocket.connect(); // Connect the websocket
pollingWebSocket.disconnect(); // Disconnect the websocket
pollingWebSocket.onMessage((data) => {
  // Handle incoming messages
});

Package Sidebar

Install

npm i client-websockets

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

15.5 kB

Total Files

7

Last publish

Collaborators

  • fedevcoding_