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

mqtt-emitter

v4.1.0

Published

Emit MQTT events to listeners using mqtt-regex for routing

Downloads

939

Readme

mqtt-emitter

This is a library for easily routing MQTT traffic in JavaScript. The API is the same as EventEmitter, but it automatically routes your topics behind the scenes. The parsing of topic patterns is done by mqtt-regex. This library can be used in Node, Browserify, or as a drop in JS file for the browser.

A goal of this library is to leave the actual MQTT transport up to the user so that they can use whatever MQTT connection they want for the actual transport, and just relay all incoming messages to this library for routing. Take a look at MQTT.js for node and Mows for the browser.

Example

Here's an example of how you can use the library to handle traffic from a hypothetical chat based on MQTT.

var MQTTEmitter = require("mqtt-emitter");

var events = new MQTTEmitter();

events.on("user/+name/message/#path", function(data, params) {
	var name = params.name;
	var path = params.path;
	console.log(name, "@", "/" + path.join("/"), ":", data);
});

var messages = ["hi", "what's up?", "Hey I'm new.", "How about that weather, eh?", "I know"];
var names = ["Bob", "Angelina", "xX420xNoSc0peXx"];
var count = 10;

function say_something() {
	var event = "user/" + random_from_list(names) + "/message/lobby";
	events.emit(event, random_from_list(messages));
	if (count--) setTimeout(say_something, 1000);
}

say_something();

function random_from_list(list) {
	return list[Math.floor(Math.random() * list.length)];
}

Installing & Usage

In node or browserify install with $ npm install --save mqtt-emitter

And then use it with

var MQTTEmitter = require("mqtt-emitter");
var events = new MQTTEmitter();

If you aren't using Browserify (for some crazy reason), then you can build a bundle with npm run build or npm run build-min. The bundle is UMD compatible and supports UMD, CommonJS and creates a global called MQTTEmitter if neither of those are present. Just dump the file in your HTML and you can use it with

var events = new MQTTEmitter();

API

The API is the same as EventEmitter except that on(), and emit() have custom functionality.

emitter.on(topic_pattern,callback)

topic_pattern is a MQTT topic with optional named wildcards which get parsed out. See mqtt-regex for how the patterns work.

callback takes four arguments:

  • payload : The payload that was passed in with emit()
  • params : The parameters parsed out using the topic pattern from the topic. See mqtt-regex for details
  • topic : The topic that was emitted with emit()
  • topic_pattern : The topic pattern that was used when this listener was registered

There are also hooks that you can listen for new and removed topic listeners.

// Override the default behavior with a custom callback for subscribing to topics
emitter.onadd = function (topic) {
	mqtt.subscribe("topic");
}
emitter.onremove = function (topic) {
	mqtt.unsubscribe("topic");
}

Note: Topic patterns are converted into their regular MQTT counterparts so foo/+boar/# is the same as foo/+baz/#. They both turn into foo/+/#

Note: Because of the nature of this library, the newListener and removeListener events aren't supported. Use the onadd() and onremove() hooks instead.

Note: Try to avoid topics with trailing slashes since they can lead to strange behavior.