Giter VIP home page Giter VIP logo

react-native-aws-iot-device-shadows's Introduction

aws-iot-device-sdk-js-react-native

React Native Component wrapper for connecting to AWS IoT from a device using SDK JavaScript bundle

Allows developers to use the AWS IOT shadow support from a React Native component.

Overview

This document provides instructions on how to install and configure the AWS IoT device/Shadow in React Native.

AWS SDK Dependency

This package is built on top of the AWS SDK aws-sdk.js which provides two classes: 'device' and 'thingShadow'.

Thing Shadows

The 'thingShadow' class implements additional functionality for accessing Thing Shadows via the AWS IoT API; the thingShadow class allows devices to update, be notified of changes to, get the current state of, or delete Thing Shadows from AWS IoT. Thing Shadows allow applications and devices to synchronize their state on the AWS IoT platform.

Device

The 'device' class wraps mqtt.js to provide a secure connection to the AWS IoT platform and expose the mqtt.js interfaces upward. It provides features to simplify handling of intermittent connections, including progressive backoff retries, automatic re-subscription upon connection, and queued offline publishing with configurable drain rate.

Installation

Installing with npm:

npm install react-native-aws-iot-device-shadows

Examples

MQTT React Native IoT Shadow Class

<AWSIoTMQTT
    ref={(ref) => { this.AWSIoTMQTT = ref; }}
    type="shadow"
    region="us-west-2"
    host="asdasd.iot.aws.com"
    onReconnect={() => this.onConnect()}
    onConnect={() => this.onConnect()}
    onDelta={(thingId, stateObject) => this.onDelta(thingId, stateObject)}
    onStatus={(thingId, statusType, clientToken, stateObject) =>
        this.onStatus(thingId, statusType, clientToken, stateObject)}
    onThingConnected={(thingId) => { this.onThingConnected(thingId); }}
/>

MQTT React Native IoT device Class

<AWSIoTMQTT
    ref={(ref) => { this.AWSIoTMQTT = ref; }}
    type="device"
    region="us-west-2"
    host="asdasd.iot.aws.com"
    onReconnect={() => this.onConnect()}
    onConnect={() => this.onConnect()} 
/>

AWSIoTMQTT

Returns a React Native component whichs wraps xxxxxxxx

## API Documentation

  • type: use 'device' for device type and 'shadows' for ShadowThing
  • host: the AWS IoT endpoint you will use to connect
  • region: the AWS IoT region you will use to connect
  • config: extra configuration for the thingShadow
  • onConnect: callback for when the websockets connects
  • onReconnect: callback for when the websockets reconnects
  • onDelta: callback for delta msg
  • onStatus: callback for status msg
  • onThingConnected: callback for each registered thing

Connection Types

This react native component only supports one type of connections to the AWS IoT platform:

  • MQTT over WebSocket/TLS with SigV4 authentication using port 443

Debug

The enable debug mode for display logging information just pass a object with debug:true

<AWSIoTMQTT
    config={debug:true}
    ...
/>

Re-Creating the bundle with webpack

This IOT JS SDK is packaged withwebpack, because currently there is not official support for AWS IOT react native. This is already bundle it for you using the last version.

npm run build

Unit Tests

This package includes unit tests which can be run as follows:

npm test

Running the unit tests will also generate code coverage data in the 'reports' directory.

License

This react native component is distributed under the Apache License, Version 2.0, see LICENSE.txt and NOTICE.txt for more information.

Support

feel free to open any ticket in github issues

react-native-aws-iot-device-shadows's People

Contributors

jamesjara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-native-aws-iot-device-shadows's Issues

Invalid connect options supplied

Hello!

I'm new using react-native and I'd like to develop a mobile application in react-native to interact with an AWSIoT shadow. I'm trying to use your lib but when I follow your example code I get this error:

screen shot 2017-11-24 at 16 43 58

my code can be seen below:

import React, {Component} from 'react';
import {
    Platform,
    Text,
    View,
    Button
} from 'react-native';
import styles from './styles'
import AWSIoTMQTT from '../../../node_modules/react-native-aws-iot-device-shadows/index.js';

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',
});

const accessKeyId = 'my_IAM_key_ID?';
const secretAccessKey = 'my_IAM_secret_key_ID?';
const sessionToken = ' ';

class DefaultScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            answer: ' ',
            device: {
                deviceId: "my_device_id",
                temp: 80
            }
        };
        this.AWSIoTMQTT = null;
    }

    componentDidMount() {
        // trigger STS credentials from Cognito Pool
        this.AWSIoTMQTT.shadow.updateWebSocketCredentials(
            accessKeyId,
            secretAccessKey,
            sessionToken
        );
    }

    onConnect() {
        this.AWSIoTMQTT.addThing(this.device.deviceId);
    }

    onDelta(thingId, stateObject) {
        if (stateObject.state.device && stateObject.state.device.temp) {
            this.updateState(thingId, {
                temp: stateObject.state.temp
            });
        }
    }

    updateShadow(key) {
        const current = this.data.device;

        const update = {
            [key]: ((Math.floor(Math.random() * (99 - 1)) + 1))
        };

        this.AWSIoTMQTT.shadow.update(current.deviceId, {
            state: {
                desired: update
            }
        });
    }

    onThingConnected(thingId) {
        this.AWSIoTMQTT.shadow.get(thingId);
    }

    updateState(thingId, sensors) {
        Object.assign(this.device, sensors);

        this.setState({
            device
        });
    }

    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>
                <Text>
                    {this.state.answer}
                </Text>

                <AWSIoTMQTT
                    ref={(ref) => { this.AWSIoTMQTT = ref; }}
                    type="shadow"
                    region="us-east-2"
                    host="my_host"
                    onReconnect={() => this.onConnect()}
                    onConnect={() => this.onConnect()}
                    onDelta={(thingId, stateObject) => this.onDelta(thingId, stateObject)}
                    onStatus={(thingId, statusType, clientToken, stateObject) =>
                        this.onStatus(thingId, statusType, clientToken, stateObject)}
                    onThingConnected={(thingId) => { this.onThingConnected(thingId); }}
                />
                <Button
                    title = 'Update Shadow Randomly'
                    onPress={() => this.updateShadow('temp')}
                />
            </View>
        );
    }
}

export default DefaultScreen

If you have any advice or tip to not hesitate to share it!
Thank you in advance!!

onClose && onOffline ?

Hello,

I'm making a RN app and I would like to use a serverless infrastructure so I naturaly found your module.
I see in index.js that we have callbacks for those events but I don't see them in the doc.
Is there any particular reason for not encouraging users to use them ?
I'm not familiar at all with IoT and MQTT protocol.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.