mqtt-connect
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 = var MQTTConnect = var settings = port: 1883 backend: type: 'mongo' url: 'mongodb://localhost:27017/mqtt' pubsubCollection: 'ascoltatori' mongo: {} var server = settings;var app = server server app // fired when a message is receivedserver // fn(packet, client)
Client Example
var mqtt = var MQTTConnect = var client = mqtt client var app = client app app client // 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
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:
// Clientapp // Brokerapp
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 app
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:
// Clientclient // fn(topic, msg) // Brokerserver // 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