简体   繁体   中英

AWS IOT Error: premature close at onclosenexttick

I am trying to build a basic AWS IOT Javascript application, below is the source code

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

var device = awsIot.device({
keyPath: 'xxx-private.pem.key',
certPath: 'xxx-certificate.pem.crt',
caPath: 'AmazonRootCA1.pem',
clientId: 'xxx_policy',
host: 'xxxxx.amazonaws.com'
});

device.on('connect', function() {
console.log('connect');
//device.subscribe('topic_1');
device.publish('MyConnectPolicy', JSON.stringify({ test_data: 'NodeJS server connected...'}));
});

device.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});

On running this project I am getting below error

events.js:200
throw er; // Unhandled 'error' event
^
Error: premature close
at onclosenexttick (xxx/node_modules/end-of-stream/index.js:54:86)
at processTicksAndRejections (internal/process/task_queues.js:76:11)

Emitted 'error' event on DeviceClient instance at:
at MqttClient. (xxx/node_modules/aws-iot-device-sdk/device/index.js:772:15)
at MqttClient.emit (events.js:228:7)
at TLSSocket.f (xxx/node_modules/once/once.js:25:25)
at onclosenexttick (xxx/node_modules/end-of-stream/index.js:54:73)
at processTicksAndRejections (internal/process/task_queues.js:76:11)

Can someone address this, thanks in advance.

I recently had this same issue when trying to connect to a thing on AWS using the AWS-IoT-SDK for node. I finally found my solution on this [Github issue][ https://github.com/aws/aws-iot-device-sdk-js/issues/304#issuecomment-608500683] .

I didn't attach a policy to my thing .

To create a new policy for your thing you could do it while creating the thing or

In the left navigation pane, choose Secure, and then choose Policies. On the You don't have a policy yet page, choose Create a policy.

On the Create a policy page, in the Name field, enter a name for the policy (for example, MyIotPolicy). Do not use personally identifiable information in your policy names.

Further reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM