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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@electrum-cash/web-socket

v4.1.1

Published

@electrum-cash/web-socket implements the ElectrumSocket interface using web sockets.

Downloads

7,809

Readme

Electrum-Cash Web Socket

This package provides a configurable ElectrumSocket that works in both NodeJS and browser settings, with consistent behavior across various environments by default.

Features

the ElectrumSocket is a wrapper for a WebSocket with the following added features:

  • Has promise-based and idempotent interfaces.
  • Uses the debug package for configurable logging levels.
  • Initial connections time out rather than linger in a stalled state.
  • Can automatically suspend to save battery based on browser visibility and connectivity.

Usage

Imports

Before using this package, you need to import it:

import { ElectrumWebSocket } from '@electrum-cash/web-socket';

To properly handle all possible errors, also import the custom error types:

import type {
	SocketInternalError,
	SocketTimeoutError,
	SocketConnectionError,
	SocketWriteError,
} from '@electrum-cash/web-socket`;

Creating an ElectrumWebSocket

First, create an instance of the socket with:

// Create the web socket with default behavior.
const socket = new ElectrumWebSocket('hostNameOrIPNumber');

Alternatively, you can configure the socket behavior by providing one or more options:

// Optionally configure socket behavior with one or more of the ElectrumSocketOptions properties.
const options =
{
	enforceConsistentBrowserBehavior: false,
}

// Create the web socket with customized behavior.
const socket = new ElectrumWebSocket('hostNameOrIPNumber', options);

Connecting to the host

After you have created an ElectrumWebSocket, you can connect with the host:

await socket.connect();

Disconnecting from a host

When you no longer need the ElectrumWebSocket, you can disconnect from the host:

await socket.disconnect();

Sending messages

While connected, you can send messages with:

await socket.write('YourTextMessageHere');

Receiving messages

To receive messages from the socket, listen to the data emitted events.

// Set up handler for incoming data.
const onSocketData = async function(data: string)
{
	console.log(data);
}

// Register handler for incoming data.
socket.addEventListener('data', onSocketData);

Managing connection

The socket also emits connected and disconnected event over its lifetime:

// Signal indicating that the socket has connected.
socket.addEventListener('connected', onSocketConnected);

// Signal indicating that the socket has disconnected.
socket.addEventListener('disconnected', onSocketDisconnected);

Handling errors

When connecting the socket, you might get a promise rejection containing an error:

try
{
	await socket.connect();
}
catch (error)
{
	// Internal errors are bugs in the implementation, simply log and move on.
	if(error instanceof SocketInternalError)
	{
		console.error(error);
	}

	// Timeout during connection could be network or host problems, try again later?
	if(error instanceof SocketTimeoutError)
	{
		// Code to try again later
	}

	// Connection errors range from being something you can handle, to things outside of your control.
	if(error instanceof SocketConnectionError)
	{
		// Inspect error.message for more details, or just log, like this:
		console.error(error);
	}
}

When writing to the socket, you might get a promise rejection containing an error:

try
{
	await socket.write('message');
}
catch (error)
{
	// Internal errors are bugs in the implementation, simply log and move on.
	if(error instanceof SocketInternalError)
	{
		console.error(error);
	}

	// Write errors means the data was not sent, so maybe try again later?
	if(error instanceof SocketWriteError)
	{
		// Code to try again later
	}
}

Change Log

v4.0

Safety

  • The following calls are now idempotent: connect(), disconnect(), write().
  • The following calls now return a promise that resolves or reject when the action is completed: connect(), disconnect().
  • The following calls are now safe to use concurrently: connect(), disconnect(), write().
  • There is now four new custom errors: SocketInternalError, SocketTimeoutError, SocketConnectionError and SocketWriteError
  • Event listeners are now assigned in only one place, and ensured not to create duplicates.
  • Added edge-case and error handling tests.
  • Added @throws to all calls that can throw.

Features

  • There is now four new utility functions to return current state: isConnecting(), isConnected(), isDisconnecting and isDisconnected.
  • Self-signed certificates are now allowed when enforceBrowserConsistency is set to false, and running in a non-browser environment.

Cleanups

  • removed error events as these are now handled with promise rejections.
  • removed support for sending Uint8Arrays, as the electrum protocol only sends strings.

v1.2

  • Handle browser visibilty and connectivity events consistently.
  • Added basic happy-path tests.

v1.1

  • Changed library bundler to tsdown

v1.0

  • Initial release