Skip to content

Instantly share code, notes, and snippets.

@andreisoriga
Created August 9, 2018 12:30
Show Gist options
  • Select an option

  • Save andreisoriga/9ca6a80b5adfe282dd9e97efdc9562cc to your computer and use it in GitHub Desktop.

Select an option

Save andreisoriga/9ca6a80b5adfe282dd9e97efdc9562cc to your computer and use it in GitHub Desktop.
MQTT.js client implementation
// https://github.com/mqttjs/MQTT.js
// https://gist.github.com/learncodeacademy/3a96aa1226c769adba39
let mqtt = require('mqtt');
let client = mqtt.connect('mqtt://localhost');
const subscriberList = ['info', 'actions'];
client.on('connect', () => {
console.log(`Subscribing to: ${subscriberList}`);
client.subscribe(subscriberList, (err, granted) => {
if (err) {
console.log('Could not connect to Mosquitto server.');
} else {
console.log('Subscribed successfully.');
}
});
});
client.on('message', (topic, message) => {
let msg = message.toString();
console.log(msg);
});
client.on('reconnect', () => {
console.log('Mosquitto client is reconecting');
});
client.on('error', err => {
console.log(`Client encountered an error ${err}`);
});
client.on('close', () => {
console.log('Client disconnected');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment