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

@takenpilot/client-side-event-bus

v1.0.0

Published

A lightweight event bus for client-side JavaScript, inspired by AMQP and Postal.js. Easily manage communication between components with a simple and efficient pub/sub architecture.

Downloads

8

Readme

Client-side Event Bus

Client-side communication with a lightweight and lightning-fast event bus.

The Client-side Event Bus is a tiny pub/sub event bus designed for seamless communication in various environments, including browsers, embedded systems, AWS Lambdas, or CloudFlare workers. Whether you're building a dynamic web application or orchestrating microservices, this event bus simplifies message handling and enhances inter-component communication.

license

Key Features:

  • Topic-based Routing: Effortlessly route messages based on custom topics.
  • Wildcard Subscriptions: Use trie-based wildcard subscriptions for flexible event handling.
  • Memory-efficient and Performant: Optimize memory usage and achieve high performance with our streamlined implementation.
  • No Dependencies: Enjoy hassle-free integration without worrying about additional dependencies.
  • Self-contained in an IIFE: Encapsulate functionality within an Immediately Invoked Function Expression (IIFE) for easy deployment.
  • Faster Than Regex-based Routing: Benefit from faster event routing compared to regex-based alternatives.
  • Browser Compatibility 8 Years+: Ensure broad compatibility with modern and legacy browsers alike.

This is a simple event bus that replicates the basics of a message broker in about 170 lines of code minified to 1437 bytes.

Subscriptions build a directed graph of tokens. Emitted events tranverse the graph to find subscriber functions to trigger.

How to use

Getting started with the Client-side Event Bus is quick and easy. Simply use emit and on methods like a regular event emitter:

var channel = new Bus();

channel.on("metrics.#", (msg) => console.log(msg));

channel.emit("metrics.page.loaded", "hello world");

Need to unsubscribe from events? No problem. Use the returned function:

var off = channel.on("page.load.justOnce", (msg) => {
  console.log(msg);
  off();
});

Gain insights into triggered events with the second parameter:

var off = channel.on("page.ad.*.filled", (msg, meta) => {
  console.log(meta.topic + "just happened");
});

Error Propagation and Results

Support RPC-like functionality by allowing errors to propagate from subscribers back to publishers. Easily disable this feature by creating an 'error' subscriber:

bus.on("some.topic", () => {
  // ...
});
bus.on("error", (error) => {
  console.log("something happened", error);
});

Retrieve results from subscribers, enabling emitters to receive Promises:

bus.on("some.topic", () => Promise.resolve(42));

await Promise.all(bus.emit("some.topic"));

The returned values can also be used to learn how many subscribers were triggered.

bus.on("some.topic", () => 42);

bus.emit("some.topic").length; // 1

Zero or more words wildcard: #

Match any number of words in a topic.

channel.on("#.changed", (msg) => {
  // ...
});
channel.emit("what.has.changed", event);
channel.on("metrics.#.changed", (msg) => {
  // ...
});
channel.emit("metrics.something.important.has.changed", event);

Single word wildcard: *

Match a single word in a topic.

channel.on("ads.slot.*.filled", (msg) => {
  // ...
});
channel.emit("ads.slot.post-nav.filled", { data, msg });

History

Keep track of event names and timestamps with a built-in history feature. Useful for performance optimization and debugging distributed systems, the history feature is ideal for tracking early page loading events and UI interactions.

Query the history using the history method:

var channel = new Bus();

channel.emit("ads.slot.post-nav.filled", { data, msg });

var history = channel.history("ads.slot.*.filled");

The format is an array of arrays. For example:

[
  ["ads.slot.post-nav.filled", 1515714470550],
  ["ads.slot.side-rail.filled", 1515714470559],
  ["ads.slot.instream-banner-0.filled", 1515714500268],
  ["metrics.component.absdf2324.render.start", 1515714782584],
  ["metrics.component.absdf2324.render.end", 1515714790507]
]

Explore Examples and More

Visit our Examples Page for common code patterns and use cases with event buses.

Related Documents

Related Topics