Skip to content

Instantly share code, notes, and snippets.

@mdobson
Created February 28, 2014 16:25
Show Gist options
  • Save mdobson/9274047 to your computer and use it in GitHub Desktop.
Save mdobson/9274047 to your computer and use it in GitHub Desktop.
Argo + Mosca = HTTP API MQTT
var argo = require('argo');
var mosca = require('mosca');
var settings = {
port: 1883
};
var server = new mosca.Server(settings);
server.on('ready', function() {
console.log('ready');
});
server.on('published', function(packet, client) {
console.log('published', packet.payload, 'to:', packet.topic);
});
argo()
.use(function(handle) {
handle('request', function(env, next) {
if(env.request.method === 'POST') {
var path = env.request.url;
env.request.getBody(function(e, b) {
if(e) {
env.response.statusCode = 500;
next(env);
} else {
b = b.toString();
var message = {
topic: path,
payload: b,
qos: 0,
retain: false
};
server.publish(message, function() {
env.response.statusCode = 201;
next(env);
});
}
});
} else {
env.response.statusCode = 404;
next(env);
}
});
})
.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment