MeteMQ Thing library for Node.js
Install MeteMQ Thing JS package
npm i --save metemq-thing-js
var Thing = require('metemq-thing-js').Thing;
var thing = new Thing('MY_FIRST_METEMQ_THING_JS');
var sub = thing.subscribe('demo')
sub.on({
added(name, age) {
console.log(`${name}(${age})`);
}
});
thing.call('hello', function(result) {
console.log(`hello ${result}!`);
});
const temp = thing.bind('temp');
setInterval(function() {
temp.set(Math.random());
}, 2000);
thing.actions({
print(c, ...args){
console.log(`message from server: ${args}`);
c.done();
}
})
Constructor
thing.bind()
thing.bind#set()
thing.call()
thing.actions()
thing.subscribe()
thing.subscribe#onEvent()
thing.subscribe#onAdded()
thing.subscribe#onChanged()
thing.subscribe#onRemoved()
thing.subscribe#on()
thing.subscribe#onAny()
thing.subscribe#unsubscribe()
thing.unsubscribe()
Initialize New Thing
var Thing = require('metemq-thing-js').Thing;
var thing = new Thing('demo_thing', {
username: 'user01',
password: 'secret',
url: 'mqtt://metemq.com'
});
Data binding
var temperBind = thing.bind('temperature');
Set bound data
var data = 'data_from_sensor';
temperBind.set(data);
Method Call (MeteMQ Remote Procedure Call)
thing.call('serversFunction');
Set Action Function
thing.actions({
toggleLed(c, params) {
doSomething(params);
c.done();
}
});
If you want to represent the progress in percentage,
thing.actions({
toggleLed(c, params) {
doSomething1(params);
c.progress(50); // 50% done
doSomething2(params);
c.done();
}
});
Function for MQTT subscription
var sub = thing.subscribe('pub_name', function() {
// successfully subscribed.
});
Listener for specific event
sub.onEvent("anEvent", function (payload) {
doSomething(payload);
})
Listener for Added
event
sub.onAdded(function (payload) {
doSomething(payload);
})
Listener for Changed
event
sub.onChanged(function (payload) {
doSomething(payload);
})
Listener for Removed
event
sub.onRemoved(function (payload) {
doSomething(payload);
})
thing.subscribe#on(handlers: { added?: Function, changed?: Function, removed?: Function }): Subscription
Listener for three environments - added
, changed
, and removed
sub.on({
added(payload) {
doOnAdded (payload);
},
changed(payload) {
doOnChanged(payload);
},
removed(payload) {
doOnRemoved(payload);
}
})
Listener for ANY event
sub.onAny(function (payload) {
doSomething(payload);
})
Function to MQTT unsubscribe
(same with thing.unsubscribe())
sub.unsubscribe(function() {
// successfully unsubscribed.
});
Function to MQTT unsubscribe
(same with thing.subscribe#unsubscribe())
thing.unsubscribe('pub_name', function() {
// successfully unsubscribed.
});
npm test
Or, in order to watch
gulp watch