Created
February 28, 2014 16:25
-
-
Save mdobson/9274047 to your computer and use it in GitHub Desktop.
Argo + Mosca = HTTP API MQTT
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
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