import Aws from 'aws-sdk/dist/aws-sdk-react-native'
import AwsIot from 'aws-iot-device-sdk'
AWS_REGION = 'us-east-1' // Change if needed.
AWS_COGNITO_IDENTITY_POOL = 'us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
// Fill in.
AWS_IOT_ENDPOINT = 'XXXXXXXXXXXXX.iot.us-east-1.amazonaws.com' // Fill in.
Aws.config.region = AWS_REGION
Aws.config.credentials = new Aws.CognitoIdentityCredentials({
IdentityPoolId: AWS_COGNITO_IDENTITY_POOL
})
Aws.config.credentials.get(() => {
const config = {}
let client
config.host = AWS_IOT_ENDPOINT
config.protocol = 'wss'
config.clientId = `client-${Math.floor((Math.random() * 100000) + 1)}`
config.accessKeyId = Aws.config.credentials.accessKeyId
config.secretKey = Aws.config.credentials.secretAccessKey
config.sessionToken = Aws.config.credentials.sessionToken
client = AwsIot.device(config)
client.on('connect', () => {
client.subscribe('some_topic')
})
client.on('message', (topic, message) => {
console.log(topic, message)
})
client.on('error', error => {
console.log(error)
})
})
https://github.com/aws/aws-iot-device-sdk-js/issues/86#issuecomment-371159865