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

core.io-pubsub-mqtt

v0.8.0

Published

core.io PubSub transport layer providing MQTT suppor

Downloads

40

Readme

core.io PubSub MQTT

This package is part of the [core.io][https://github.com/goliatone/application-cor] set of libraries, meaning it can be used independently or inside an core.io application.

To install:

$ npm i -S core.io-pubsub-mqtt

Documentation

Request / Response Flow

This module provides a request/response flow that you can leverage to replicate HTTP mechanics. The following illustrates a simple example

  • client.js:
pubsub.request('ci/registry/list').then(response => {
    console.log(response.message) //This is my response
}).catch(error => {
    //handle error
});
  • server.js:
pubsub.subscribe('ci/registry/list', event => {
    //...perform logic to generate a result object
    let result = { message: 'This is my response' };
    event.respond({ result });
});

If you are using the package as a core.io module and using the dispatch/command flow, you can configure the module to behave use core.io dispatcher handling events with a respondTo function so that your commands do not have to worry about specifically handling the respond call.

Your application setup:

./commands/api.image.get.js

class ApiImageGetCommand {
    execute(event) {
        return {
            success: true,
            message: 'This is my command response'
        };
    }
}

./config/pubsub.js:

module.exports = {
    responseTopicKey: 'responseTopic',
    responseCallerKey: 'respondTo',
    handlers: {
        'mqtt/api/image/get': function handler(topic message) {
            const context = this;
            context.emit('api.image.get', message);
        }
    }
};

Then from your application dispatching the request:

let res = await pubsub.request('api.image.get', {filename});

Response Middleware

You can add response middleware to build the payload of your response object. Note that the middleware will be applied before applying any transformations done in during the payload publish.

In your configuration file:

  • config/pubsub.js:
module.exports = {

    /**
     * These functions will be called in the
     * order they are added
     */
    responseMiddleware: [
        function(response, data, error) {

            if(data && !error) {
                response.data = data;
                response.success = true;
            }

            if(error && !data) {
                response.error = error;
                response.success = false;
            }

            return response;
        },

    ]
};

Publish Payload Transformers

You can modify the payload before being published over MQTT. By default we have two transformers, one to add a timestamp and one to add a uuid to the emitted payload.

Ensure UUID

UUID transform to ensure event payloads have a unique ID.

Default implementation will add a field named uuid.

Configuration keypath: pubsub.transforms.ensure.uuid.

You can disable this transformer by setting the value of the configuration keypath to false.

Configuration options:

  • fieldName: String representing the payload field name, defaults to uuid.
  • getId: function to generate the ID. Default to uuid.v4()

Ensure Timestamp

Timestamp transform to ensure event payloads have a timestamp.

Default implementation will add a field named timestamp.

Configuration keypath: transforms.ensure.timestamp.

You can disable this transformer by setting the value of the configuration keypath to false.

Configuration options:

  • fieldName: String representing the payload field name, defaults to timestamp.
  • getTimestamp: Function to generate the timestamp. Default to Date.now()

License

® License MIT 2017 by goliatone

TODO:

  • [ ] Implement a pin/pong flow so that we can easily build on top of it