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-regex

v1.1.0

Published

Converts an MQTT topic with parameters into a regular expression.

Downloads

827

Readme

mqtt-regex

Converts an MQTT topic with parameters into a regular expression.

Unless you need regex support, you should use mqtt-pattern which is faster

Example

var mqtt_regex = require("mqtt-regex");

var pattern = "chat/+id/+user/#path";

var room_message_info = mqtt_regex(pattern).exec;

var topic = "chat/lobby/bob/text";

var message_content = "Hello, World!";

var params = room_message_info(topic);

if(params && (params.path.indexOf("text") !== -1)) {
	chat.getRoom(params.id).sendFrom(params.user, message_content)
}

Installing

With npm:

$ npm install --save mqtt-regex

To use it in the browser, either compile a package with node run build or use Browserify.

API

The API is super simple and should be easy to integrate with any project

mqtt_regex(topic_pattern)

Takes an MQTT topic pattern, and generates a RegExp object along with a function for parsing params from the result. The results also have an exec function that does both. The return looks like

{
	regex: "RegExp object for matching"
	getParams: function(results){
		// Processes results from RegExp.prototype.exec
		// Returns an object containing the values for each param
	},
	exec: function(topic){
		// Performs regex.exec on topic
		// If there was a match, parses parameters and returns result
	},
	topic: "Regular MQTT topic with the pattern stuff stripped out",
	format: function(params){
		// Generate a string with the params filled into the pattern
		return "formatted/pattern/here
	}
}

How params work

MQTT defines two types of "wildcards", one for matching a single section of the path (+), and one for zero or more sections of the path (#). Note that the # wildcard can only be used if it's at the end of the topic. This library was inspired by the syntax in the routers for Express and Sinatra, and an attempt was made to have this just as simple to use.

Examples of topic patterns:

user/+id/#path

This would match paths that start with user/, and then extract the next section as the user id. Then it would get all subsequent paths and turn them into an array for the path param. Here is some input/output that you can expect:

user/bob/status/mood: {id: "bob", path:["status","mood"]
user/bob: {id:"bob", path: []}
user/bob/ishungry: {id: "bob", path: ["ishungry"]

device/+/+/component/+type/#path

Not all wildcards need to be associated with a parameter, and it could be useful to just use plain MQTT topics. In this example you might only care about the status of some part of a device, and are willing to ignore a part of the path. Here are some examples of what this might be used with:

device/deviceversion/deviceidhere/component/infrared/status/active: {type:"infrared",path: ["status","active"]}