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

v1.0.1

Published

MQTT high level framework to glue together various middleware to handle incoming messages

Downloads

8

Readme

mqtt-connect NPM Version Build Status JavaScript Style Guide

MQTT high level framework to glue together various "middleware" to handle incoming messages.

It works for both MQTT Broker and Client.

Heavily inspired by the HTTP connect module form-factor.

Broker Example

var mosca = require('mosca')
var MQTTConnect = require('mqtt-connect')

var settings = {
  port: 1883,
  backend: {
    type: 'mongo',
    url: 'mongodb://localhost:27017/mqtt',
    pubsubCollection: 'ascoltatori',
    mongo: {}
  }
}

var server = new mosca.Server(settings);
var app = new MQTTConnect.broker(server)

server.on('clientConnected', function(client) {
    console.log('client connected', client.id)
})

app.use('/topic', function(broker, client, packet, next){
  // middleware
  console.log(packet.payload)
  next()
})

// fired when a message is received
server.on('published', app.stack) // fn(packet, client)

Client Example

var mqtt = require('mqtt')
var MQTTConnect = require('mqtt-connect')

var client  = mqtt.connect('mqtt://test.mosquitto.org')

client.on('connect', function () {
  client.subscribe('presence')
  client.publish('presence', 'Hello mqtt')
})

var app = new MQTTConnect.Client(client)

app.use(function middleware1(client, msg, next) {
  // middleware 1
  next()
})

app.use(function (client, msg, next) {
  // middleware 2
  console.log(msg.topic)
  console.log(msg.data) // buffer
  client.publish('boop/', 'boop')
})

client.on('message', app.stack) // fn(topic, msg)

Mount middleware

The .use() method also takes an optional path string that is matched against the beginning of the incoming request topic. This allows for basic msg routing:

app.use('/topic1', function fooMiddleware(client, msg, next) {
  // your middleware logic
  next()
})

API

app.use(fn)

Use a function on the app, where the function represents a middleware. The function will be invoked for every request in the order that app.use is called. The function is called with three arguments for the client and 4 arguments for the Broker:

// Client
app.use(function (client, msg, next) {
  // client is an MQTT client instance
  // msg is an object with 2 property: topic <String>, data <Buffer>
  // next is a function to call to invoke the next middleware
})

// Broker
app.use(function(broker, client, packet, next){
  // broker is a reference to our broker.
  // client is the sender mqtt client.
  // packet is the incoming packet, containes: .topic, .payload etc.
  // next is a function to call to invoke the next middleware
})

app.use(topic, fn)

Use a function on the app, where the function represents a middleware. The function will be invoked for every packet received in which the TOPIC match with the given topic string in the order that app.use is called.

app.use('/foo', function (client, msg, next) {
  // client is an MQTT client instance
  // msg is an object with 2 property: topic <String>, data <Buffer>
  // next is a function to call to invoke the next middleware
})

app.use('/bar/+', brokerCb)

The topic could be terminated with a path separator (/) or an MQTT wildcard character (+ or #). This means the given topic /foo/ and /foo are NOT the same and both will not match the same messages.

Moreover the topic is matched in a case-sensitive manor!

Check the mqtt-match module or the MQTT protocol documentation to better understand topic matching.

app.stack

Expose the function that will iterate through the added middlewares instances.

  • For the Client could be called with 2 params (topic,msg).
  • For the Broker could be called with 2 params (packet,client).

Common usage:

// Client
client.on('message', app.stack) // fn(topic, msg)

// Broker
server.on('published', app.stack) // fn(packet, client)

It's built to be compatible with the MQTT.js Module and the Mosca Broker. But it could also be used together with an EventEmitter class that emits messages following the same function signature.

app.getCount()

Returns the number of middlewares currently inside our MQTTConnect Client or Broker instance.

app.reset()

Removes all the middlewares from our app.

Author

Rocco Musolino (@roccomuso)

License

MIT