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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ws-handler

v1.2.0

Published

A websocket handler to make a connection with multiple entities attached to a single connection.

Downloads

0

Readme

WS-Handler

WS-Handler is a library based on WebSockets to handle cases where we need to attach multiple entities to a single connection.

Install

npm install ws-socket

Usage

Import

Code for importing the methods in your page

import {webSocketHandler,entryAdded,entryCancelled,entryDeleted,entryUpdated} from 'ws-handler';

Set App Name, ID Array and URL

The entity name is sent during the creation of the connection and the idArray is used to send the list of attached entities to the same connection. The URL is the endpoint url of the server.

const entityName = "entityName"
const [idArray,setIdArry] = React.useState([])
const URL = url

Declare a function

This function will be used to get the event object from the socket handler.

function socketHandler(event){
	if(event.type == "open"){
		setIsConnected(true);
	}
	else if(event.type == "close"){
		setIsConnected(false);
	}
	else if(event.type == "message"){
		console.log(event.data);
	}
	else if(event.type == "error"){
		console.log("Error Occured");
	}
}

Here the function socketHandler is accepting an event object and the type property of this object determines the connection state. The four different type values are

  • open > suggests that connection was successful
  • close > suggests that the connection was closed
  • message > suggests that a message has been received from the server
  • error > there was an error during the process

On successfully receiving a message, we can access the same from data property of the event.

Calling the Methods

Establishing a Connection

For sending a connection request, we need to use the webSocketHandler method of the library as follow

const connectionObject = {"URL" : URL, "entityName" : entityName, "idArray" : idArray}
webSocketHandler(connectionObject,{socketHandler})

Here we called the module and send two parameters

  • First parameter is an object containing the entityName and idArray
  • Second parameter is the function which will be handling the events received from the library

Now it will be better to write this in useEffect hook as whenever the idArray will change the subscribed list of entities will be sent again to the backend. So the complete code for establishing a connection will be

useEffect(async () => {
	const connectionObject = {"URL" : URL, "entityName" : entityName, "idArray" : idArray}
	webSocketHandler(connectionObject,{socketHandler});
},[idArray])

Adding an Entity

When we add an entity and needs to notify the server for the same we use entryAdded method

entryAdded({"eventData" : data})

Deleting an Entity

When we add an entity and needs to notify the server for the same we use entryAdded method

entryDeleted({"eventData" : data})

Updating an Entity

When we add an entity and needs to notify the server for the same we use entryAdded method

entryUpdated({"eventData" : data})

Cancelling an Entity

When we add an entity and needs to notify the server for the same we use entryAdded method

entryCancelled({"eventData" : data})

NOTE : These methods expects an object as a parameter having property eventData. The data passed as a value of this property is an JSON object. entryDeleted and entryUpdated method expects an "ID" property inside the data object.