VictorHAS / mqtt-react-hooks

ReactJS library for Pub/Sub communication with an MQTT broker using Hooks
MIT License
91 stars 28 forks source link

Unable to use variables or objects for connection #51

Closed inteldon closed 2 years ago

inteldon commented 2 years ago

I'm trying to do the following in React with the mqtt-react-hooks:

This is an example of the function that I'm using (note the comments for additional info)

export default function MqttConn(){
    const url_works = "ws://mymqttserver:myport/mqtt"; // This works 
    const url_doesntwork = variable_from_json_config_file; // This doesnt work 

    // I need to call a function to get each users login info, so "myOptions" is initially blank 
    const [myOptions, setState]=useState({
        username:"",
        password:"",
        clientId:"",
        keepAlive: 60, 
        clean: true
      });

    useEffect(()=>{
        My_Function_Checks_Server('someFunction', (error,response)=>{
            if(e){console.log(e)}
            //--- 
            setState({
                username: response.mqttUser,
                password: response.mqttPass,
                clientId: response.mqttTopic
            });
            //---  
        });        
    }, []); 

    return (
        <Connector brokerUrl={url_works} options={myOptions}>
            <Status/>
            <div>
                Test area for "setState" {/* This proves that all variables are fetched and correct*/} 
                <ul>
                    <li> Username: {myOptions.username} </li>
                    <li> Password: {myOptions.password} </li>
                    <li> ClientId: {myOptions.clientId} </li>
                </ul> 
            </div> 
        </Connector>
    )
}

My observations are:

Without these features this plugin cannot scale, so can you please guide me on how to use the variables for this hook, whether through a json config or a server side function call?

Thank you

inteldon commented 2 years ago

It's possible that the values are being updated, but the connection is not being initiated after the values are updated. I checked and console.log(ed) the updated options, but the <Connector> component might not be updating. Just a thought.

If that's the case, how can we make the connector attempt a new connection based on the new data?

Note: I have resolved the URL through working around the problem (string splits and joins) so as far as I'm concerned it's working for me.

inteldon commented 2 years ago

Closing because there's no response from the developers.