Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
The qlobber npm package is a fast and flexible topic-based publish/subscribe library for Node.js. It allows you to match topics to subscribers using a trie-based approach, which is efficient and scalable. This package is particularly useful for implementing message brokers, event emitters, and other pub/sub systems.
Basic Topic Matching
This feature allows you to add topics and match them against patterns. The code sample demonstrates how to add topics and retrieve matches for specific topics.
const Qlobber = require('qlobber').Qlobber;
let matcher = new Qlobber();
matcher.add('foo.*', 'it matches foo.bar');
matcher.add('foo.*', 'it also matches foo.baz');
matcher.add('foo.bar', 'it matches foo.bar exactly');
console.log(matcher.match('foo.bar')); // ['it matches foo.bar', 'it matches foo.bar exactly']
console.log(matcher.match('foo.baz')); // ['it also matches foo.baz']
Removing Topics
This feature allows you to remove specific topics from the matcher. The code sample demonstrates how to add and then remove a topic, showing the updated matches.
const Qlobber = require('qlobber').Qlobber;
let matcher = new Qlobber();
matcher.add('foo.*', 'it matches foo.bar');
matcher.add('foo.bar', 'it matches foo.bar exactly');
matcher.remove('foo.bar', 'it matches foo.bar exactly');
console.log(matcher.match('foo.bar')); // ['it matches foo.bar']
Wildcard Matching
This feature supports wildcard matching using '*' and '#' symbols. The code sample demonstrates how to use wildcards to match multiple topics.
const Qlobber = require('qlobber').Qlobber;
let matcher = new Qlobber();
matcher.add('foo.*', 'it matches foo.bar');
matcher.add('foo.#', 'it matches foo.bar and foo.baz');
console.log(matcher.match('foo.bar')); // ['it matches foo.bar', 'it matches foo.bar and foo.baz']
console.log(matcher.match('foo.baz.qux')); // ['it matches foo.bar and foo.baz']
The mqtt package is a client library for the MQTT protocol, which is a lightweight messaging protocol for small sensors and mobile devices. It provides similar pub/sub functionality but is more focused on network communication and IoT applications. Unlike qlobber, mqtt is protocol-specific and includes features for connecting to MQTT brokers.
The eventemitter2 package is an enhanced version of Node.js's EventEmitter class. It provides advanced features like wildcard event names, which are similar to qlobber's topic matching. However, eventemitter2 is more general-purpose and is used for event handling within applications rather than for message brokering.
The postal package is a JavaScript library for pub/sub messaging. It offers similar topic-based subscription and publication features. Postal is more focused on in-app messaging and provides additional features like message channels and middleware, making it more versatile for complex applications compared to qlobber.
Node.js globbing for amqp-like topics.
Example:
Qlobber = require('qlobber').Qlobber;
matcher = new Qlobber();
matcher.add('foo.*', 'it matched!', function ()
{
matcher.match('foo.bar', function (err, vals)
{
assert.deepEqual(vals, ['it matched!']);
});
});
The API is described here.
qlobber is implemented using a trie, as described in the RabbitMQ blog posts here and here.
npm install qlobber
A more advanced example using topics from the RabbitMQ topic tutorial:
async.parallel(
[matcher.add.bind(matcher, '*.orange.*', 'Q1'),
matcher.add.bind(matcher, '*.*.rabbit', 'Q2'),
matcher.add.bind(matcher, 'lazy.#', 'Q2')],
async.mapSeries.bind(async,
['quick.orange.rabbit',
'lazy.orange.elephant',
'quick.orange.fox',
'lazy.brown.fox',
'lazy.pink.rabbit',
'quick.brown.fox',
'orange',
'quick.orange.male.rabbit',
'lazy.orange.male.rabbit'],
matcher.match,
function (err, vals)
{
assert.deepEqual(vals,
[['Q1', 'Q2'],
['Q1', 'Q2'],
['Q1'],
['Q2'],
['Q2'],
[],
[],
[],
['Q2']]);
}));
qlobber passes the RabbitMQ topic tests (I converted them from Erlang to Javascript).
To run the tests:
grunt test
grunt lint
Source: lib/qlobber.js
Creates a new qlobber.
Parameters:
{Object} [options]
Configures the globber. Use the following properties:
{String} separator
The character to use for separating words in topics. Defaults to '.'. MQTT uses '/' as the separator, for example.
{String} wildcard_one
The character to use for matching exactly one word in a topic. Defaults to '*'. MQTT uses '+', for example.
{String} wildcard_some
The character to use for matching zero or more words in a topic. Defaults to '#'. MQTT uses '#' too.
{String | false} compare
The function to use for sorting matches in order to remove duplicates. Defaults to lexicographical string compare. Specify false
to turn off duplicate removal. If you store values other than strings in qlobber, pass in your own compare function.
Go: TOC
Add a topic matcher to the qlobber.
Note you can match more than one value against a topic by calling add
multiple times with the same topic and different values.
Parameters:
{String} topic
The topic to match against.{Any} val
The value to return if the topic is matched.{Function} cb
Called when the matcher has been added.Go: TOC | QlobberObject
Remove a topic matcher from the qlobber.
Parameters:
{String} topic
The topic that's being matched against.{Any} val
The value that's being matched.{Function} cb
Called when the matcher has been removed.Go: TOC | QlobberObject
Match a topic.
Parameters:
{String} topic
The topic to match against.
{Function} cb
Called with two arguments when the match has completed:
{Any} err
null
or an error, if one occurred.{Array} vals
List of values that matched the topic. vals
will be sorted and have duplicates removed unless you configured Qlobber otherwise.Go: TOC | QlobberObject
—generated by apidox—
FAQs
Node.js globbing for amqp-like topics
The npm package qlobber receives a total of 193,267 weekly downloads. As such, qlobber popularity was classified as popular.
We found that qlobber demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.