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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ezez/mqtt

v3.0.0

Published

An `mqtt` package wrapper that adds features I feel that should be part of the original package.

Downloads

51

Readme

Easy mqtt wrapper

An mqtt package wrapper that adds features I feel that should be part of the original package.

I have little time for development, so this is incomplete, not every function is wrapper properly, so be careful.

Features

Topic-based subscriptions

With bare mqtt library you have to do this:

client.subscribe("mytopic", (err) => {
    if (err) {
        // handle the error
        return;
    }
    client.on("message", (topic, message) => {
        if (topic === "mytopic") {
            // do something with the message
        }
    });
});

client.subscribe("other", (err) => {
    if (err) {
        // handle the error
        return;
    }
    client.on("message", (topic, message) => {
        if (topic === "other") {
            // do something else
        }
    });
});

// or use one `client.on("message")` and still filter topic manually

With this lib you can:

client.sub("mytopic", (message) => {
    // do something with the message
});
client.sub("other", (message) => {
    // do something else
});

Amazing, isn't it?

Of course if you want to handle two different topic with the same callback you can do the usual:

client.sub(["mytopic", "other"], (message) => {
    // do something with both topics
});

This function also explicitly throws when subscription fails, so you avoid unnoticed errors in your app.

To stop receiving messages from a topic you can super easily unsubscribe from it:

const unsubscribe = client.sub("mytopic", (message) => {
    // do something with the message
});

setTimeout(unsubscribe, 5000); // stop receiving messages after 5 seconds

Topic prefix

If you are developing and running production app on a single mqtt instance you may want to prefix all your topics with something, so you can easily switch between different instances. This lib allows you to do that:

const client = createMQTT(process.env.MQTT_CONNECTION_STRING, "test");

client.publish("mytopic", "message"); // will publish to "test/mytopic"

Subscribing will also subscribe you into prefixed topics, and they will be stripped when you receive a message.

client.on("message", (topic, message) => {
    console.log(topic); // will print "mytopic" if something was published to "test/mytopic"
});

Increased modularity by requiring to unsubscribe as many times as you subscribed

With bare mqtt library if you subscribe to a topic twice (ie. two different modules of your app) and you unsubscribe once, you will stop receiving messages from that topic (one of your module will "broke"). This is patched with @ezez/mqtt and now you are required you to unsubscribe as many times as you subscribed to stop receiving messages.

Additional utils

With MQTT (protocol, not the library) you can't read when the message was published, this is especially painful with retained messages (I store i.e. current temperature as retained, so I can always immediately access it, but I also need to know how up to date this information is). So I usually post my messages with a timestamp.

I also make other parts of my app or related devices to post heartbeats to a topic, so I know they are still alive.

There are two functions that can help you with that:

  • now function publishes current timestamp to a topic
  • publishNow function expects an object and adds a timestamp to it as a time property before publishing

Getting started

Instead of importing mqtt library and calling mqtt.connect you should use createMQTT function from this package.

import { createMQTT } from "@ezez/mqtt";

const client = createMQTT(process.env.MQTT_CONNECTION_STRING, optionalPrefix, optionalOptions);

Then use the usual features or the new ones on the client.

License

MIT

Other

Versions prior to 3.0.0 were published as @dzek69/mqtt and now are deprecated.