Created
August 9, 2018 12:30
-
-
Save andreisoriga/9ca6a80b5adfe282dd9e97efdc9562cc to your computer and use it in GitHub Desktop.
MQTT.js client implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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