aws / aws-iot-device-sdk-js

SDK for connecting to AWS IoT from a device using JavaScript/Node.js
Apache License 2.0
964 stars 384 forks source link

Do not see inside the onMessage function, you are periodically updated it #314

Closed Angelk90 closed 3 years ago

Angelk90 commented 4 years ago

I have a state that I use to keep information, the problem is that after I receive the first time a message, it seems that inside the onMessage function, I always see it in the same way, that is, as it was before receiving the first message.

That is:

StateOriginal

I get a message

I change an obj inside the state

I get a message

The state I see is the original one, not the one with the changes

You know give me a hand?

Code:

import React from "react";
import "./styles.css";

export default function App() {

  const [state, setState] = React.useState({ message: [], iot: undefined, ... });

  React.useEffect(() => {
    const iot = getIotDevice({
      IdentityId,
      Credentials: Aws.config.credentials
    });
    setState(prev => ({ ...prev, iot }));
    return () => {
    if (iot) iot.end();
};
  }, []);

  const getIotDevice = ({ IdentityId, Credentials }) => {
        const device = new AwsIot.device({
            protocol: 'wss',
            secretKey: Credentials.secretAccessKey,
            accessKeyId: Credentials.accessKeyId,
            clientId: IdentityId,
            host: config.aws.mqttHost,
            sessionToken: Credentials.sessionToken,
            clean: true,
            resubscribe: true
        });
        console.log('getIotDevice', state);
        device.on('connect', onConnect);
        device.on('message', onMessage);
        device.on('error', onError);
        device.on('offline', onDisconnect);
        device.on('close', onClose);
        return device;
    };

/*
    function onMessage(topic, message) {
        let msg;
        try {
            msg = JSON.parse(message.toString());
        } catch (err) {
            msg = message.toString();
        }
        console.log('Msg', msg, state);
    }
*/

const onMessage = (topic, message) => {
        let msg;
        try {
            msg = JSON.parse(message.toString());
        } catch (err) {
            msg = message.toString();
        }
        console.log('Msg', msg, state);
    }

  return null;
}
KaibaLopez commented 4 years ago

Hi @Angelk90, I'm not super clear on what you mean by changing the status, but the message function is usually unrelated to device status, as in it doesn't check or update the status, so unless you call for that manually you won't get that. You can call something like on('delta') that would do something on status change, or on('status') and play with that see if that works for you?

Angelk90 commented 4 years ago

Hi @KaibaLopez , in a nutshell, I receive messages, do I have to add these messages to the state up to here we are?

KaibaLopez commented 4 years ago

Sorry, I don't understand your question. But I don't think you got that right, the idea is that you won't get a change in device state by sending a message.

jmklix commented 3 years ago

@Angelk90 Do you still have any questions regarding message state?

github-actions[bot] commented 3 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.