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

gptto-pubsub

v0.1.1

Published

Low level implementation of zeromq pub sub sockets.

Downloads

2

Readme

So after playing around with last iteration of Centrum-Messengers, I realized I had some unoptimized abstractions that were quite useful, but definitely not needed. I'm building a game engine (Gotti.js) and didnt want those abstractions anymore so decided to take out a lot of it and was left with a very simple wrapper around the zmq pub/sub sockets. Since it's for use of the game engine I'm buidling, I'm renaming it. Also plan on doing the same with CentrumChannels.

I wanted to be able to get the most optimal performance out of them and promoting the use of json, although very user friendly, is slower than just designing things around protocols and formatting your arrays.

It's bare bones and very low level but can be powerful if used correctly.

After initializing your Messenger instance with and/or pub and socket configurations, it's very very trivial to pub and sub to messages.

The only thing a publisher really is, is a name that SHOULD identify some sort of protocol, all that means is the name allows the subscriber to know what process it should run on the publication.. it's literally just another way to say "What should the subscriber do (handler function) when it receives a publication with the name/protocol 'ADD' "

can be used to call the publisher and the subscriber listens for the messages.

    messenger.createPublish('ADD')

then used like -

    messenger.publications.ADD([5, 10]);

now all you have to do on the receiving end, is register the subscription for the name/protocol with the following API -

    messenger.createSubscription(protocolOrName, handlerId, handler)

or

    messenger.createOrAddSubscription(protocolOrName, handlerId, handler)

The only difference between the two is 'createSubscription' will through an error if you try to register a subscription handler with the same name more than once. 'createOrAddSubscription' can have multiple handlers.

heres an example of how messenger.createSubscription would look if we were making it in response to the publication we defined above.

    messenger.createSubscription("ADD", "unique", (data) => {
        console.log('output:' + (data[0] + data [1]) );
    })

logs out -

    output: 15

Remember we sent as data [5, 10], so all it does is add those two values.. and it knows to do that because it was the "ADD" protocol. Don't overthink it, it's really just basically the node event emitter with sockets.

or if you want multiple handlers

    for(let 1 = 0; i <= 5; i++) {
        let handlerId = ('id_' + i);
        messenger.createOrAddSubscription("ADD", handlerId, (data) => {
            console.log(`${handlerName}:${(data[0] + data[1] + i)}`);
        })
    }

logs out-

    id_0: 15
    id_1: 16
    id_2: 17
    id_3: 18
    id_4: 19

then to remove any of these subscriptions you have the following functions

     removeSubscriptionById(id, name);
     removeAllSubscriptionsWithId(id);
     removeAllSubscriptionsWithName(name);
     removeAllSubscriptions();

You can also chose to create publications without encoding if you want to encode it separately somewhere in your application. All you have to do is pass a second parameter in createPublication with false;

    messenger.createPublish('ADD', false);

then do the same thing for the subscription

    messenger.createSubscription('ADD', 'ID', (data) => {
        JSON.parse(data);
    }, false);