Skip to content

Instantly share code, notes, and snippets.

@toddtreece
Last active March 8, 2016 14:20
Show Gist options
  • Save toddtreece/feb66ddef602c8ec0c1e to your computer and use it in GitHub Desktop.
Save toddtreece/feb66ddef602c8ec0c1e to your computer and use it in GitHub Desktop.
var mqtt = require('mqtt'),
client = mqtt.connect('mqtt://phantmqtt.sparkfun.com'),
pub = 'vGArXg1lPgi4yDPZElO1ilyrEeB',
prv = 'XpzBK84Xq8Fg3xrOa9NRIEPJ4DV';
client.subscribe('response/' + pub + '/input'); // responses about input
client.subscribe('output/' + pub); // JSON encoded full payload
client.subscribe('output/' + pub + '/b'); // only subscribe to one field
// log everything to the terminal
client.on('message', function(topic, message) {
console.log('TOPIC: ' + topic, ' PAYLOAD: ' + message);
});
// log data
client.publish('input/' + pub + '/' + prv, JSON.stringify({
a: 1,
b: 'test'
}));
{
"name": "mqtt-client-test",
"version": "0.0.0",
"description": "test client for phantmqtt.sparkfun.com",
"main": "client.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"mqtt": "^0.3.11"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment