1.0.4 • Published 5 years ago

mqtt-agent v1.0.4

Weekly downloads
24
License
-
Repository
-
Last release
5 years ago

This is a MQTT Agent

It is a simplification of MQTT in NodeJS. All functions return a native-promise that you can use Asynchronously.

Installation

$ npm install --save mqtt-agent

Usage

const mqttAgent = require('mqtt-agent');

// const agent = new MQTTAgent('mqtt://yourmqttserver:8883'); // Default message callback
const agent = new MQTTAgent('mqtt://your-mqtt-server:8883', (topic, msg) => {
  // Do something with your Topic and Message.
}); // Custom message callback

agent.connect() // Return a promise when it is connected to your server
  .then((connected) => {
    console.log('Connected:', connected); // Do something with the connected status
    // You can now subscribe, publish and all with native promises!
    return agent.subscribe('your_topic/your_device/something'); // Returns a promise
  })
  .then((subscribedTopic) => {
    console.log('Topic subscribed to:', subscribedTopic);
    // You can send options as last parameter
    return agent.publish('yourTopic', 'yourMessage can be a JS Object or string'); 
  })
  .then((published) => {
    console.log('Published (boolean):', published); // Then if you are subscribed to the topic, you will receive the message in your message callback
    return agent.end(); // Disconnect
  })
  .then((disconnected) => {
    console.log('Disconnected:', disconnected);
  })
  .catch((error) => {
    console.log('Error:', error);
  });

License

© Javier Cañadilla

1.0.4

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago