Angular Integrate AWS IoT device sdk

#1

Hello 各位大神安安

因為工作上需求需要收AWS IoT Mqtt的資料,
請問有人整合過AWS IoT device sdk嗎?


我照著他的範例去整合一直都會出現類似以下的錯誤:
Uncaught TypeError: process.nextTick is not a function
at Object.nextTick (index.js:26)
at resume (_stream_readable.js:783)
at Duplexify.push…/node_modules/readable-stream/lib/_stream_readable.js.Readable.resume (_stream_readable.js:775)
at Duplexify.push…/node_modules/readable-stream/lib/_stream_readable.js.Readable.on (_stream_readable.js:745)
at Duplexify.push…/node_modules/readable-stream/lib/_stream_readable.js.Readable.pipe (_stream_readable.js:615)
at MqttClient.push…/node_modules/aws-iot-device-sdk/node_modules/mqtt/lib/client.js.MqttClient._setupStream (client.js:274)
at new MqttClient (client.js:227)
at new DeviceClient (index.js:639)
at Response. (dashboard.component.ts:156)
at Request. (request.js:364)

明明有透過aws sdk拿到credentials了但卻一直無法成功建立mqtt client
目前code裡面有個問題就是sample是使用
const awsIot = require(‘aws-iot-device-sdk’)
mqttclient = awsIot.device(deviceOption)
因為Typescript就是會一直叫我new …所以我的寫法是
import awsIot from ‘aws-iot-device-sdk’;
mqttclient = new awsIot.device(deviceOption)

找了好久…還是找不到為什麼…上來求救各位大神
看有沒有人有整合過的經驗可以分享qq
感謝

0個讚

#2

問題後來解決了,但主要問題不是process.nextTick的問題
分享一下後續…給未來要串接的朋友採雷時可以有點依據
使用Angular串接aws-iot-device-sdk這個library,
在npm i aws-sdk , npm i aws-iot-device-sdk , npm @types/aws-iot-device-sdk 後
可能會遇到幾個問題

  • build code fail : Can’t resolve ‘fs’ 、Can’t resolve ‘tls’、Can’t resolve ‘path’…
    =>網路上查會有人叫你在package.json裡面的browser node加上
    browser:{
    fs:false,
    path:false,
    tls:false
    }
    這個是錯的…會衍生出下面的問題
  • Uncaught ReferenceError: global is not defined
    =>網路上查會叫你在polyfills.ts增加(window as any).global = window;.
  • Uncaught ReferenceError: Buffer is not defined
    =>網路上查會叫你在polyfills.ts增加global.Buffer = global.Buffer || require(‘buffer’).Buffer;
  • 都加完後就會出現本篇問題裡面Uncaught TypeError: process.nextTick is not a function
    =======================================================================
    實際上在build code fail 這個問題時,還有另一個解答才能真正解答問題…
    https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d
    需要到 node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js
    裡面將原本的node:false改成
    node: {
    crypto: true,
    stream: true,
    fs: ‘empty’,
    tls: ‘empty’
    }
    這樣就能解決全部的問題也能成功串接了…但這個是node_modules裡面的檔案
    目前也不知道該如何完全解決,但連結裡面是教我們寫一個patch.js去改裡面的檔案
    怕忘記的朋友也可以像他寫一個patch或寫在readme裡面提醒一下新來同事…
1個讚

#3

以下是我串接成功之後的code…不過剛串接成功並沒有整理過xd,有興趣的朋友可以參考就好

import AWS from 'aws-sdk/global';
import { CognitoIdentity } from 'aws-sdk';
import awsIot from 'aws-iot-device-sdk';
linkToAWS() {
    const topic = 'topic/you/want';
    AWS.config.region = this.awsConfiguration.region;

(AWS.config
  .credentials as AWS.CognitoIdentityCredentials) = new AWS.CognitoIdentityCredentials(
  {
    IdentityPoolId: this.awsConfiguration.poolId
  }
);
(AWS.config.credentials as AWS.CognitoIdentityCredentials).get(err => {
  if (err) {
    console.log('error retrieving identity:' + err);
    alert('error retrieving identity: ' + err);
    return;
  }

  console.log(
    'credentials.identityId',
    (AWS.config.credentials as AWS.CognitoIdentityCredentials).identityId
  );
  // get credentials for this identity
  const params = {
    IdentityId: (AWS.config.credentials as AWS.CognitoIdentityCredentials)
      .identityId
  };
  const cognitoIdentity = new CognitoIdentity();
  cognitoIdentity.getCredentialsForIdentity(params, (err, data) => {
    if (err) {
      console.log('error retrieving credentials: ' + err);
      alert('error retrieving credentials: ' + err);
      return;
    }
    console.log('data', data);

    const clientId =
      'mqtt-client-' + Math.floor(Math.random() * 100000 + 1);

    const mqttClient = new awsIot.device({
      region: AWS.config.region,
      host: this.awsConfiguration.host,
      clientId,
      protocol: 'wss',
      maximumReconnectTimeMs: 8000,
      debug: true,
      accessKeyId: data.Credentials.AccessKeyId,
      secretKey: data.Credentials.SecretKey,
      sessionToken: data.Credentials.SessionToken
    });
     mqttClient.on('error', error => {
       console.log('error');
     });

    mqttClient.on('connect', () => {
      console.log('connect');
      mqttClient.subscribe(topic);
    });
    mqttClient.on('message', (data, message) => {
      console.log('message', data, message.toString());
    });
  });
});

}

2個讚