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=;var host='mqtt://localhost';//Standard settings available in npm package mqttvar settings= protocolId: 'MQIsdp' protocolVersion: 3;/** * The last parameter should be true for an always active connection, false for a one time connection * @type */var resmetry= hostsettingsfalse; /** 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 listenerresmetry; //Message event listenervar temp=25;resmetry; //Making a request to topic 'request' with message 'send me details' whose expected result goes to topic response with optionsvar options=qos:2;//Options are standard options available for publishing in npm package mqttresmetry;
output:
Connected
Topic: request/1 Message: temp
Topic: response/1 Message: 25
Response:25
API
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 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 for information on events and other functionalities.
resmetry.disconnect()
Disconnects the active MQTT client connection
Events
### 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