mqtt-collection package for Meteor
-
Messages received via MQTT broker are written into collection
-
Data inserted into collection is broadcasted via MQTT
Example
Connect to MQTT broker and subscribe to topic (server side only)
MyCollection.mqttConnect("mqtt://test.mosquitto.org", ["presence"], {}, {});
We are now connected and subscribed to "presence" topic. Anything published to "presence" MQTT topic will be written into MyCollection.
Broadcast data (works both on client and server)
MyCollection.insert({ topic: "presence", message: "Hello world! :)", broadcast: true });
You need to insert three mandatory fields: topic
, message
and broadcast
and your message will be broadcasted via MQTT broker to specified topic.
Functions
Collection.mqttConnect(uri, topics, options, mqttOptions)
Establishes connection to MQTT broker and subscribes to listed topic(s).
Arguments:
uri
is mqtt broker addresstopics
is array of strings or single string - topic name(s) to subscribe on connectoptions
is object with following properties:
{ insert: false, raw: false }
insert
- if set to true, each message will be inserted into collection (and your collection will grow!). If this option is not set (or set to false) messages will be upsert-ed (you'l have single document for each topic). Default: falseraw
- if set to true, received string will be written as-is. If this option is not set (or set to false) received string will be converted to object withJSON.parse()
. Default: falsemqttOptions
is an object that is supplied tomqtt.connect([url],options)
in the MQTT.js library for configuring the underlying options of the MQTT.js-client. See the docs.
Collection.mqttDisconnect()
Closes connection to MQTT broker
Collection.mqttSubscribe(topics)
Subscribe to specified topic(s). Works only after MQTT connection is established.
Arguments:
topics
is array of strings or single string - topic name(s) to subscribe
Live example
You can find example application using this package here.
That's all folks :)