Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

ProxyLayer Between MQTT Broker (Any MQTT Broker) and AWS IoT Broker

5.00/5 (2 votes)
10 Feb 2016CPOL 8.9K  
ProxyLayer Between MQTT Broker (Any MQTT Broker) and AWS IoT Broker

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

AWS IoT Broker currently supports only X.509 certificate Authorization. If your device does not support X.509, use this ProxyLayer to fill the Gap.

It listens to MQTT broker (Setup any broker, e.g., Mosquitto, HiveMQ) and publish that message to AWS IoT broker.

Create proxylayer.js file and paste the following code into it:

JavaScript
var mqtt = require('mqtt'), url = require('url');
var client = mqtt.connect('mqtt://127.0.0.1:1883',
{
username: '<mqtt username>',
password: '<mqtt password>'
});

var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({

keyPath: ‘&lt;Certificate key file path&gt;’,
certPath:‘&lt;Certificate file path&gt;’,
caPath: '‘&lt;Certificate root file path&gt;’,
clientId: ‘&lt;AWS Thing Name&gt;’,
region: ‘&lt;AWS IoT Broker region&gt;’
});

device.on('connect', function ()
{
//Write logging to check connection done or not
});

client.on('connect', function()
{
//subscribe to a topic (#)
client.subscribe('#', function ()
{
client.on('message', function (topic, message, packet) {
console.log("Received :-" + message + " on " + topic);
device.publish(topic, message);
console.log("Sent :-" + message + " on " + topic);
});
});
});

Run this into nodejs server by running the following command:

  • to run it from console: node proxylayer.js
  • to run it as background service: forever proxylayer.js (first installed forever by using npm -g install forever)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)