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

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.


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?