npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ws-rekanet

v0.4.0

Published

Simple WebSocket wrapper with reconnect logic

Downloads

3

Readme

ws-rekanet

Publish

Simple WebSocket wrapper with reconnect logic

Motivation

The Websocket API although effective and simple doesn't provide any reconnection logic. This library aims to solve this shortcoming by providing a mechanism to shore up session_affinity between the client [a web browser] and the server. It also provides a simple queue implementation where data being sent while the connection is broken are stored, to be relayed when connection is restored.

Note that the library is just a wrapper for WebSocket class and re-exposes all it's API. It's also built on an event interface which allows it to emit native WebSocket events. Finally, it's properly tested using AVA.

installation

# yarn
yarn add ws-rekanet

# pnpm
pnpm install ws-rekanet

# npm
npm install ws-rekanet

Usage

import WSRekanet from "ws-rekanet";

const url = `ws://localhost:3001/`;
const protocols = [];
const options = {
  maxReconnectAttempts: 5,
  maxRetryAttempts: 3,
  useMessageQueue: true,
};

// initialize
const wsClient = new WSRekanet(url, protocols, options);

wsClient.on("open", () => {
  console.log("websocket connection established");
});
wsClient.on("message", (event: any) => {
  wsResponse = String(event.data);
});
wsClient.on("error", (error: any) => {
  console.log("websocket error", error);
});
wsClient.on("close", () => {
  console.log("websocket connection closed");
});

const payload = { message };
wsClient.send(JSON.stringify(payload));

API

url

Type: string

protocols

Type: string | string[] Default: []

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). See more MDN, WhatWG#web-sockets

options

Type: object

maxReconnectAttempts

Type: number
Default: 5

Number of times it attempts to reconnect within a retry

maxRetryAttempts

Type: number
Default: 5

The maximum number of retries - how many attempts at reconnecting

useMessageQueue

Type: boolean
Default: true

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

disableReconnect

Type: boolean
Default: false

Whether to disable reconnection

debugMode

Type: boolean
Default: false

Whether to run in debug mode which enables console.log output

Events

open, close, message, error

These are the same as native WebSocket events

reconnection_timeout

Indicates when reconnection attempts timeout.

reconnected

Emitted when reconnection attempt is successful - connection restored