1.0.9 • Published 7 years ago

resmetry v1.0.9

Weekly downloads
13
License
MIT
Repository
github
Last release
7 years ago

Resmetry is a client library for the MQTT protocol, written in JavaScript for node.js with the aim of building request-response communication on top of mqtt protocol. Resmetry supports both on request connection and always-on connection.

This library relies on MQTT.js library for interacting with mqtt server.

Installation

npm install resmetry --save

Example

var resmetrylib=require('resmetry');
var host='mqtt://localhost';
//Standard settings available in npm package mqtt
var settings={
  protocolId: 'MQIsdp',
  protocolVersion: 3
};
/**
 * The last parameter should be true for an always active connection, false for a one time connection
 * @type {resmetry}
 */
var resmetry= new resmetrylib(host,settings,false);

/*
* Get mqtt client for advanced operations, Refer npm package mqtt for more information
* For other features offered by MQTT, the modifications can be made using the object
* Only applicable true is passed the last parameter of the constructor
*/
//var mqtt=resmetry.getMQTTClient();
//MQTT operations
//mqtt.subscribe('request/1');

//Connection listener
resmetry.on('connect',function(message){
  console.log(message);
});

//Message event listener
var temp=25;
resmetry.on('message',function(topic,message){
  console.log("Topic: "+topic,"Message: "+message);
  if(topic==='request/1'&&message==='temp'){
    mqtt.publish('response/1',temp+'',{qos:2});
  }
});

//Making a request to topic 'request' with message 'send me details' whose expected result goes to topic response with options
var options={qos:2};
//Options are standard options available for publishing in npm package mqtt
resmetry.request('request/1','temp',options,'response/1',function(err,response){
  console.log('Response:'+response);
});

output:

Connected
Topic: request/1 Message: temp
Topic: response/1 Message: 25
Response:25

## API

  • new resmetry()
  • resmetry.request()
  • resmetry.getMQTTClient()
  • resmetry.disconnect()

new resmetry(host,settings,connectionType)

Connects to the broker specified by the given url and options and ted options. Options are as mentioned in connect part of npm package mqtt.js ConnectionType specifies whether current operation is one-time or continuous. One-time activated by passing false, makes the connection end after obtaining the response. Whereas, continuous operation mode activated by passing true, runs forever.


resmetry.request(topic,data,options,responseTopic,callback)

  • 'topic' is the topic to which message will be send to.Like request
  • 'data' is the message which needs to be passed along with request
  • 'options' are additional options like qos and retain features. Refer npm package mqtt.js publish
  • 'responseTopic' is the the topic to which response will be send toLike response
  • 'callback' is where any err or result will be passed

resmetry.getMQTTClient()

Returns the mqtt client for the functionality of add your listeners other than 'message' event.Refer mqtt.js Events for information on events and other functionalities.


resmetry.disconnect()

Disconnects the active MQTT client connection


## Events

  • connect
  • message
  • error

    resmetry.on('connect',function(message))

  • Fired when mqtt client connects to server

  • 'message' is a string saying Connected


    resmetry.on('message',function(topic,message))

  • Fired when a message comes

  • 'topic' is the topic to which message was published
  • 'message' contains the message send in the topic


    resmetry.on('error',function(error))

  • Fired when error is encountered

  • error contains details related to the error thrown

License

MIT

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago