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개 답변
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?

답변함 3달 전
profile picture
전문가
검토됨 한 달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠