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

mercury-broker

v1.0.0-beta.2

Published

Lightweight message broker for JavaScript

Downloads

30

Readme

mercury-broker

A lightweight message broker for JavaScript

Main features

  • Publish/subscriber semantics, with both synchronous and asynchronous event publishing
  • Message payload transformation
  • Delayed message delivery
  • Periodically message publishing
  • Content based event filtering

You should use mercury-broker because...

  • It's very light and very simple
  • It's a fully opensource solution
  • It has no external dependency: no left-pad risk
  • It's available both as npm module and as bower module

Basic usage

Require (npm) module

var hg = require('mercury-broker');

Including (bower) module

<script src="/bower_components/mercury-broker/src/main/js/broker.js"><script>
<script>
    // Global [window.]hg variable is now available
</script>

Event subscription

var unsubscribe = hg.subscribe('an-event', function(evt, payload) {
    console.log('--', evt, payload);
});

Event publication

broker.publish('an-event', 'Hello, World!');
// Output: -- an-event Hello, World!

Subscription removal

unsubscribe();

Advanced usage

Payload transformation

function toUpper(str) {
    return str.toUpperCase();
}
hg.subscribe('an-event', function(evt, payload) {
    console.log(payload);
}, { transformations: [toUpper]});
hg.publish('an-event', 'Hello, World!');
// Output: HELLO, WORLD!

Multiple payload transformations

    function toUpper(str) {
        return str.toUpperCase();
    }
    function addPrefix(str) {
        return '::' + str;
    }
    hg.subscribe('an-event', function(evt, payload) {
        console.log(payload);
    }, { transformations: [ toUpper, addPrefix ] });
    hg.publish('an-event', 'Hello, World!');
    // Output: ::HELLO, WORLD!

Asynchronous event delivering

    hg.subscribe('an-event', function() {
      called = true;
    });
    // Non-blocking event publishing: publisher doesn't wait for
    //  subscribers' processing
    hg.publish('an-event', {}, {
      async: true
    });
    console.log(called); // **false**
    setTimeout(function() {
      console.log(called); // **true**
    }, 5);

Delayed (asynchronous) event delivering

     hg.subscribe('an-event', function() {
        called = true;
     });
     // Delayed event publishing
     hg.publish('an-event', {}, {
       delay: 2000
     });
     console.log(called); // false
     setTimeout(function() {
       console.log(called); // false
     }, 500);
     console.log(called); // false
     setTimeout(function() {
       console.log(called); // false
     }, 1500);
     console.log(called); // false
     setTimeout(function() {
       console.log(called); // true
     }, 2500);

Periodic event publishing

     hg.subscribe('periodic-event', function() {
        called = true;
     });
     // Publishes 'periodic-event' every 0.2s
     hg.publish('periodic-event', {}, {
       interval: 200
     });

Periodic event publishing with payload generator function

     hg.subscribe('periodic-event', function(event, payload) {
        console.log(payload.id, payload.text);
     });
     var counter = 0;
     hg.publish('periodic-event', { text: 'Seed text' }, {
       interval: 200,
       generator: function(seed) {
         seed.id = counter;
         seed.text = seed.text + ' - ' + counter;
         counter++;
         return seed;
       }
     });
     // Output is
     // 0 Seed text - 0
     // 1 Seed text - 1
     // 2 Seed text - 2
     // 3 Seed text - 3
     // ...

Content-based event filtering

     hg.subscribe('an-event', function(event, payload) {
        console.log(payload.it);
     }, { filter: function(payload) { return payload.id % 3 === 0; }});
     // Consumes only events whose payload has id that is multiple of three
     for(var i = 0; i < 10; i++) {
        hg.publish('an-event', { id: i });
     }
     // Output is
     // 0
     // 3
     // 6
     // 9

Try hg-broker in your browser through Tonic