Skip to content

Instantly share code, notes, and snippets.

@adamvr
Last active December 14, 2015 19:18
Show Gist options
  • Save adamvr/5135589 to your computer and use it in GitHub Desktop.
Save adamvr/5135589 to your computer and use it in GitHub Desktop.
Quick and dirty http mqtt publisher
node_modules
var mqtt = require('mqtt')
, express = require('express');
var app = express();
var publish = function(topic, payload, cb) {
var client = mqtt.createClient(1883, 'test.mosquitto.org');
client.on('connect', function() {
client.publish(topic, payload, cb);
client.end();
});
};
app.use(express.bodyParser());
app.get('/', function (req, res) {
res.send('Test thing!');
});
app.post('/:param', function (req, res) {
publish(req.param, req.body, function () {
res.send(200);
});
});
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Listening on port ' + 3000);
});
{
"name": "mqtt-http",
"version": "0.0.0",
"description": "mqtt-http bridge",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "",
"keywords": [
"mqtt",
"http",
"publish",
"subscribe",
"pubsub"
],
"author": "Adam Rudd",
"license": "BSD",
"dependencies": {
"mqtt": ">=0.2.0",
"express": "~3.1.0"
},
"engines": {
"node": ">=0.6.0",
"npm": ">=1.1.0"
}
}
web: node .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment