Definitely check out, and brush up, on your Node.js-fu because it makes developing (i.e. prototyping and/or full scalable solutions) absolutely possible. This tidbit won't get in to the nitty gritty of how to maximize Node.js for multicore, however, those resources are available in the API documentation as well as other blog posts floating around the internets.
Here, I'll demonstrate a simple and effective way to utilize the Mosca framework alongside the Node.js http module to get dual MQTT:HTTP citizenship.
If you're interested to develop your own MQTT broker, and in Javascript, then look no further than Mosca. It's MQTT 3.1.1 compliant, and works with amazingly high fidelity; and in other words, it's quality stuff. Both Mosca and Node.js modules function via C++, and are pretty close to the metal. The abstraction provides plenty of bang for the buck.
Additionally, the npm dispatch module was used to provide some pretty useful routing capability. Here, the code is in single file mode: what you'd want to do is to call this server.js file from cluster, and to have your controllers tucked away neatly, and with the functions passed in lieu of the anonymous ones. Hopefully, these will get you started.
var authenticate = function (client, username, password, callback) { console.log('ping ', username); // if (username == "test" && password.toString() == "test") callback(null, true); // else // callback(null, false); } var authorizePublish = function (client, topic, payload, callback) { callback(null, true); } var authorizeSubscribe = function (client, topic, callback) { callback(null, true); } var mosca = require('mosca'); var ascoltatore = { type: 'mongo', url: 'mongodb://localhost:27017/mqtt', pubsubCollection: 'ascoltatori', mongo: {} }; var moscaSetting = { port: 1883, host: "192.168.foo.bar", // specify an host to bind to a single interface logger: { level: 'debug' }, persistence: { factory: mosca.persistence.Mongo, url: 'mongodb://localhost:27017/mqtt' }, backend: ascoltatore }; var http = require('http') , dispatch = require('dispatch') , broker = new mosca.Server(moscaSetting); httpServ = http.createServer( dispatch({ '/': function(req, res, next){ console.log('alpha romeo'); }, '/user/:id': function(req, res, next, id){ // ... }, '/user/posts': function(req, res, next){ // ... }, '/user/posts/(\\w+)': function(req, res, next, post){ // ... } }) ); broker.attachHttpServer(httpServ); httpServ.listen(3000); broker.on('ready', setup); function setup() { broker.authenticate = authenticate; broker.authorizePublish = authorizePublish; broker.authorizeSubscribe = authorizeSubscribe; console.log('Mosca broker is up and running.'); } broker.on("error", function (err) { console.log(err); }); broker.on('clientConnected', function (client) { console.log('Client Connected \t:= ', client.id); }); broker.on('published', function (packet, client) { console.log("Published :=", packet); }); broker.on('subscribed', function (topic, client) { console.log("Subscribed :=", client.packet); }); broker.on('unsubscribed', function (topic, client) { console.log('unsubscribed := ', topic); }); broker.on('clientDisconnecting', function (client) { console.log('clientDisconnecting := ', client.id); }); broker.on('clientDisconnected', function (client) { console.log('Client Disconnected := ', client.id); });
No comments:
Post a Comment