Skip to content

Instantly share code, notes, and snippets.

@yoshimax
Created July 29, 2017 10:37
Show Gist options
  • Save yoshimax/d0a1ab6eb1a6693cff390be48fc693d2 to your computer and use it in GitHub Desktop.
Save yoshimax/d0a1ab6eb1a6693cff390be48fc693d2 to your computer and use it in GitHub Desktop.
AWS Iot
// Pub Sub On Node.js
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
keyPath: 'keys/XXXXX.private.key',
certPath: 'keys/XXXXX.cert.pem',
caPath: 'keys/aws-iot-rootCA.crt',
clientId: 'max-1',
host: 'AAAAA.iot.us-east-1.amazonaws.com'
});
device
.on('connect', function() {
console.log('connect');
device.subscribe('topic_1');
device.publish('topic_2', JSON.stringify({ test_data: 1}));
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
// Test Command
$ mosquitto_pub --cafile aws-iot-rootCA.crt --cert XXXXX.cert.pem --key XXXXX.private.key -h AAAAA.iot.us-east-1.amazonaws.com -p 8883 -q 1 -d -t topic_1 -i client_max1 -m 'testMessage'
$ mosquitto_sub --cafile aws-iot-rootCA.crt --cert XXXXX.cert.pem --key XXXXX.private.key -h AAAAA.iot.us-east-1.amazonaws.com -p 8883 -q 1 -d -t topic_2 -i client_max1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment