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

@joekiller/ws-manager

v1.1.0

Published

A message caching WebSocket manager.

Downloads

2

Readme

Joe's WebSocket Manager

A simple interface to connect to a websocket and receive an event when messages are pending.

The manager works by emitting a 'messages' event as soon as messages are within its queue. Read the messages by calling .getMessages() repeatedly until no more messages are in the queue. The 'messages' event will be emitted again when the queue was empty and more messages have arrived.

Please remember: The 'messages' event will only fire the first time a message is available on the queue and will not emit again until .getMessages() has been called and there are no more messages to consume. You should always call .getMessages() until you get zero messages back. Once no messages are queued you can rely on the emission of the 'messages' event to trigger when more messages have arrived.

The manager is uses the NodeJS ws WebSockets Library.

Installing

npm install @joekiller/ws-manager

Usage

The manager will emit a 'messages' event whenever the queue was exhausted and more messages are available. manager.on('messages', fn)

Events

The following events are emitted from the manager.

type ManagerEvents = {
  messages: () => void;
  close: (code: number, reason: Buffer) => void;
  error: (err: Error) => void;  // just log this or errors will trip you up
  opened: () => void;
};

Example

import WSManager from '@joekiller/ws-manager';

const webSocketManager = new WSManager<unknown>('wss://ws.backpack.tf/events');

function run() {
  let running = true
  webSocketManager.on('error', (err: ErrorEvent) => console.log(err.error, err.message, err.type));
  webSocketManager.on('messages', () => {
    const messages = webSocketManager.getMessages();
    if(messages.length > 0 && running) {
      running = false;
      const message = messages.shift();
      console.log(JSON.stringify(message, null, 2));
      webSocketManager.shutdown();
    }
  });
  // connect after setting up to receive messages
  webSocketManager.connect();
}
run();

Notes and Features

  1. The manager will automatically reconnect to websockets with some backoff if it keeps failing to connect.
  2. The manager utilizes the WebSocket standard of Pings to detect if the line has gone silent without a disconnect.
  3. The manager allows minimal to zero polling as the consumer is always notified as soon as more messages have arrived.
  4. The manager caches all pending messages in memory for eventual consumption. With a fast enough processor, memory consumption is not a problem. However, be aware that this mechanism could cause heavy memory consumption if the consumer cannot keep up with the stream.
  5. I have run this engine with some other projects consuming over 66 million messages in a row with no memory leaks or issues. It seems pretty solid.

About Me

I made this in my spare time and I hope you find it useful. Look me up on any of the following social networks:

Please understand I will not offer any support outside GitHub issues and I make no promises to attend to those either. Happy coding!

End