Category: ProxyLayer

How to Create a Proxy Layer Between MQTT Broker & AWS IOT Broker

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

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

console.log("Connected to MQTT Broker:- localhost” + client.toString());
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({

keyPath:  Certificate key file path,
certPath: Certificate file path,
caPath:   Certificate root file path,
clientId: AWS Thing Name,
region:   AWS IoT Broker region,
});

device.on('connect', function ()
{
console.log("Connected to AWS IoT Broker:- " + device.toString());
});

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);
});
});
});