PubSub Iot Core React Native doesn't work

0

Hello, I am currently in the process of developing an Android app using Amplify and React Native. The primary goal is to implement authentication and enable the publication and subscription to topics using MQTT. To achieve this, I have employed the PubSub library. However, I have not yet achieved the desired results. Here are the steps I've taken so far:

Create android app with React Native: RESULT: OK. I can run the app on the emaultor.

Add authentication to the app: RESULT: OK. I can sign up, sign in and sign out.

Add IoT publish/subscribe for MQTT messages I have followed the instructions on: https://docs.amplify.aws/react-native/build-a-backend/more-features/pubsub/set-up-pubsub/

  1. Attach IotPolicy: aws iot attach-policy --policy-name 'myIoTPolicy' --target '<YOUR_COGNITO_IDENTITY_ID>' I use the parameter 'aws_cognito_identity_pool_id': 'eu-central-1:2fXXXXXXXXXXXX', as it appears in the amplifyconfiguration.json file, for the Cognito Identity ID. Is it correct?
  2. I have attached the policies AWSIoTDataAccess and AWSIoTConfigAccess in the AuthRole in the parent stack of my solution on CloudFormation.
  3. execute command: npm install aws-amplify @aws-amplify/pubsub

RESULT: NO OK. I test the messages with the MQTT test client at IoT Core, but nothing happens.

**The main part of my file App.js related with IoT is the following: **

import { PubSub } from '@aws-amplify/pubsub';
// Apply plugin with configuration
const pubsub = new PubSub({
  region: 'eu-central-1',
  endpoint:'wss://XXXXXXXXX.iot.eu-central-1.amazonaws.com/mqtt'
});
const App = () => {

  const subsToTopic = pubsub.subscribe({ topics: 'MyTopicELSend' }).subscribe({
    next: (data) => console.log('Message received', data),
    error: (error) => console.error(error),
    complete: () => console.log('Done')
  });

  async function pubToTopic() {
    console.log("It's publishing");
    await pubsub.publish({ topics: 'MyTopicELRcv', message: { message: 'Relay value from smartphone', relayValue: 2 } });
  }

  return (
    <View style={styles.container}>
      <SignOutButton />
      <TouchableOpacity onPress={pubToTopic} style={styles.buttonContainer}>
        <Text style={styles.buttonText}> Publish to topic</Text>
      </TouchableOpacity>
    </View>
  );
}

I don't know where the mistake is. Could you help me?

Thank you. Best regards, Antonio

1 Answer
0

I found my answer. It's not correct use the parameter 'aws_cognito_identity_pool_id': 'eu-central-1:2fXXXXXXXXXXXX', as it appears in the amplifyconfiguration.json file, for the Cognito Identity ID. You must sign in and use fetchAuthSession to find your identityID.

However, I have a new question: each logged-in user has an ID, so should I programmatically attach the myIotPolicy to each user? How can I do it? The command "aws iot attach-policy --policy-name 'myIoTPolicy' --target '<YOUR_COGNITO_IDENTITY_ID>'" is executed in the terminal AWS CLI. Could you help me?

answered 3 months ago
profile picture
EXPERT
reviewed 24 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions