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

ts-post

v2.0.0

Published

Flexible PubSub messaging bus system for node and browser applications

Downloads

6

Readme

ts-post

Install

In terminal, run:

npm i ts-post

Basic usage

ts-post allows you to create multiple buses to better segment and handle what messages the subscribers will receive or not. This encourages you to have a well defined type for message data going through each bus, helping limiting potential errors where callbacks would have tried to handle different objects than expected. The singleton instance of Port has to be created globally, in order to be accessible everywhere in your app without risk of undelivered messages. By default, a bus named default is created.

Import

import { Message, Post, Subscription } from 'ts-post';

Example

const post = Post.getInstance;

// Create a new bus called foo, which will dispatch messages only to foo subcribers
post.createBus('foo');

// Subscribe to the bus
const sub = post.subscribe('foo', {
    callback: async (message: Message) => {
        console.log(`Message timestamp: ${message.getTimestamp()} - issuer: ${message.getIssuer()}`);
        await doSomethingWithData(message.getData());
    },
    errorHandler: (error) => { console.error(error); }
});

// Publish a message into the bus
await post.publish('foo', new Message({ id: 'bar', description: 'some data', available: 104 }, 'FooService'));

// Unsubscribe
sub.unsubscribe();

Options and data

Subscriber

The available options are:

  • callback (required): the callback to execute when receiving a message
  • errorHandler (optional): the callback to execute in case of exception while executing the callback
  • delay (optional): the delay before executing the callback
    • If undefined, the callback will be executed immediatly and synchronously (according to its order in the subscribers list)
    • If >= 0, the callback will be put in the event loop using setTimeout

When subscribing, the returned subscriber can call .unsubscribe() to remove the subscription to the bus.

Message

When creating a message to be published, the options are:

  • data (required): the actual data to send
  • issuer (optional): the app component/service/... responsible for the message publishing

The data sent is packaged with additional info:

  • getData returns the data sent in the message
  • getTimestamp returns the creation time (in ms) of the message
  • getIssuer returns the issuer of the message (if defined)

Contribute

Please feel free to suggest features or bug fix through Git issues. Pull Requests for that are also more than welcome.