How to implement AWS IoT(device) in React-Native?
Asked Answered
E

2

10

I am trying to Implement AWS-IoT(device) using React-Native.

I have used the packages,

1) aws-iot-device-sdk-js

2) react-native-aws-iot-device-shadows

and got a lot of errors while using the package. I could debug few, but did not get expected results.

I am Implementing AWS-IoT for Chatting application.

I am successfully creating an IoT session using REST APIs and get these as responses iotEndpoint, region, accessKey, secretKey, sessionToken. But using these credentials I am unable to Connect using the above packages.

Egidius answered 5/12, 2018 at 13:6 Comment(3)
what is your question?Loricate
@SelmiKarim The question is how AWS IoT can be implemented in React-Native, as the expected packages specified above are throwing lot or errors.Ultrasonic
can you show us the code you tried so far.Loricate
U
12

I figured this out,

Step 1: Install aws-iot npm package npm install --save aws-sdk aws-iot-device-sdk

Step 2: Install the nodeify package npm install --save-dev rn-nodeify

Step 3: Run this command to install series of packages specified

npx rn-nodeify --install "fs,util,path,tls,stream,buffer,global,process" --hack

"Please wait until the all the packages are installed"

Step 4: Goto package.json -> in scripts section add,

"postinstall": "rn-nodeify --install fs,util,path,tls,stream,buffer,global,process --hack"

Step 5 : Install the asyncstorage-down package npm install --save asyncstorage-down

Step 6: rn-nodeify will auto-generate a file shim.js in the root part of your react-native project. Just import it in index.js file like this import './shim'

Finally, you are ready to use your aws-iot package !!!

It is advisable to generate the iot-session keys as specified in the question above using REST API's in the backend.

Ultrasonic answered 14/12, 2018 at 11:6 Comment(9)
can you tell me how to connect IoT(device) with react native?? tried aws-iot-device-sdk-js library with but got this error =>> ERROR TypeError: filesys.existsSync is not a function at module.exports (tls-reader.js:89) at new DeviceClient (index.js:455)Goner
Did you complete the entire steps as shown in the answer?. So installing aws-iot-device-sdk will raise a lot of package unknown errors. So in order to support the dependencies of aws-iot i am using rn-nodeify. Share a screenshot of error messages via link and share it in this comments. I'll look into it.Ultrasonic
here it my screenshot - drive.google.com/file/d/1Dy-ZEQHD7OewB5N4A87mT1_7YebZsuNS/view and my package.json drive.google.com/file/d/1OP1aW3Hoy8ROMQIpuiojnR6I5gfa4P8-/viewGoner
Have you imported shim file in index.js ?Ultrasonic
Let us continue this discussion in chat.Ultrasonic
can you send me your package.json ?Goner
This worked for me but i was getting "Can't find variable: Buffer" issue but then imported import './shim' in App.js file its working fine.Kissee
hi guys, why is Uncaught TypeError: filesys.existsSync happening to meFendig
when you import Buffer for React-Native you must use BufferJs; and when you import it you must use a trailing slash ie import { Buffer } from 'buffer/'. This trailing slash helps the JS transpiler know to use your NPM package not attempt to use a node native packapgeNomination
L
3

I've read this post and the chat log, but have been unable to find a solution for this issue which I also seem to have. I have followed all the steps described here by Ron, but I get a filesys.existsSync is not a function error. I have included shim import as first code line in index.js. The code for communicating with AWS is as follows.

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 * @lint-ignore-every XPLATJSCOPYRIGHT1
 */

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import AwsIot from 'aws-iot-device-sdk'

const instructions = Platform.select({
  ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
  android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type Props = {};
export default class App extends Component<Props> {
  constructor(props){
    super(props)
    this.connectToIoT()
  }

  connectToIoT(){
    var device = AwsIot.device({
       keyPath: './cert/mykey-private.pem.key',
       certPath: '/cert/mycert-certificate.pem.crt',
       caPath:   './cert/AmazonRootCA1.pem.key',
       clientId: 'myclientid',
       host: 'myhost'
   });
   console.log(device)
   device
    .on('connect', function() {
      console.log('connect');
    });
    device
    .on('message', function(topic, payload) {
      console.log('message', topic, payload.toString());
    });
    }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

Any possible solutions, or other methods of communicating with AWS IoT using MQTT in react native when using certificates for authentication?

Lyndell answered 1/3, 2019 at 12:24 Comment(1)
I met the smae thing. I set my path using react-native-fs and put my certs and keys in MainBundle. but also get Uncaught TypeError: filesys.existsSync is not a functionIncoordination

© 2022 - 2024 — McMap. All rights reserved.