Skip to content

Instantly share code, notes, and snippets.

@chrvadala
Last active September 24, 2018 16:31
Show Gist options
  • Save chrvadala/b0925d95c1927edaf2169d80c60e2726 to your computer and use it in GitHub Desktop.
Save chrvadala/b0925d95c1927edaf2169d80c60e2726 to your computer and use it in GitHub Desktop.
mqtt connect
/**
* usage node connect.js mqtt://localhost:1883
*/
const mqtt = require('mqtt')
const URI = process.argv[2]
const EVENTS = ['connect', 'reconnect', 'close', 'offline', 'end', 'error', 'packetsend', 'packetreceive', 'message']
console.log('CONNECTING', URI)
let client = mqtt.connect(
URI,
{keepalive: 120 + Math.round(Math.random() * 100)}
)
EVENTS.forEach(eventName => {
client.on(eventName, data => console.log(eventName, JSON.stringify(data)))
})
client.subscribe('#')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment