Skip to content

metemq/metemq-thing-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MeteMQ

NPM

MeteMQ Thing JS

MeteMQ Thing library for Node.js

Getting Started

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();
  }
})

API


Constructor(thingId: string, options?: ThingOptions)

Initialize New Thing

var Thing = require('metemq-thing-js').Thing;

var thing = new Thing('demo_thing', {
  username: 'user01',
  password: 'secret',
  url: 'mqtt://metemq.com'
});

thing.bind(field: string, updateFunction?: Function): Binding

Data binding

var temperBind = thing.bind('temperature');

thing.bind#set(...args)

Set bound data

var data = 'data_from_sensor';
temperBind.set(data);

thing.call(method, ...args)

Method Call (MeteMQ Remote Procedure Call)

thing.call('serversFunction');

thing.actions(actions: { [action: string]: Function })

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();
  }
});

thing.subscribe(name: string, ...args: any[]): Subscription

Function for MQTT subscription

var sub = thing.subscribe('pub_name', function() {
  // successfully subscribed.
});

thing.subscribe#onEvent(ev: string, func: Function): Subscription

Listener for specific event

sub.onEvent("anEvent", function (payload) {
  doSomething(payload);
})

thing.subscribe#onAdded(func: Function): Subscription

Listener for Added event

sub.onAdded(function (payload) {
  doSomething(payload);
})

thing.subscribe#onChanged(func: Function): Subscription

Listener for Changed event

sub.onChanged(function (payload) {
  doSomething(payload);
})

thing.subscribe#onRemoved(func: Function): Subscription

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);
  }
})

thing.subscribe#onAny(func: Function): Subscription

Listener for ANY event

sub.onAny(function (payload) {
  doSomething(payload);
})

thing.subscribe#unsubscribe(callback?: Function)

Function to MQTT unsubscribe

(same with thing.unsubscribe())

sub.unsubscribe(function() {
  // successfully unsubscribed.
});

thing.unsubscribe(name: string, callback?: Function)

Function to MQTT unsubscribe

(same with thing.subscribe#unsubscribe())

thing.unsubscribe('pub_name', function() {
  // successfully unsubscribed.
});

How to test?

npm test

Or, in order to watch

gulp watch

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published