Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mohdrashid/resmetry

Request-response operation over MQTT protocol
https://github.com/mohdrashid/resmetry

callback mqtt mqtt-client mqtt-protocol nodejs request response topic

Last synced: 20 days ago
JSON representation

Request-response operation over MQTT protocol

Awesome Lists containing this project

README

        

Resmetry is a client library for the [MQTT](http://mqtt.org/) 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](https://www.npmjs.com/package/mqtt) for interacting with mqtt server.


## Installation

```sh
npm install resmetry --save
```


## Example

```js
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](https://www.npmjs.com/package/mqtt#connect)
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](https://www.npmjs.com/package/mqtt#publish)
* 'responseTopic' is the the topic to which response will be send to[Like 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](https://www.npmjs.com/package/mqtt#connect) 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