Introvertuous / react-native-mqtt

Mqtt client for react native.
MIT License
156 stars 31 forks source link

[QUESTION] - Store Paho.MQTT.Client in REDUX store #16

Open dittmarconsulting opened 6 years ago

dittmarconsulting commented 6 years ago

Hi there,

I have successfully implemented the package and love it. Great work.

Right now I have stored the client object in the local store as shown in the example but when I try to store the object in the REDUX store I get only an empty object.

Here a simple example

constructor(props) {
        super(props)

        const client = new Paho.MQTT.Client(<IP>, <PORT>, <CLIENT ID>)
        client.onConnectionLost = this._onConnectionLost
        client.onMessageArrived = this._onMessageArrived
        client.connect({
            onSuccess: this._onConnect,
            useSSL: true
        })
        this.props.setMQTTClient(client)

Where setMQTTClient() is the action that invokes the reducer to write it to the client to the store.

Since I'm getting an empty object I'm wondering whether the client creation is Promised based. If yes, how can I hook on the callback so that I can save the entire object once it has been created.

idlesong commented 6 years ago

+1

cltsang commented 5 years ago

There is not quite enough information to look into your problem. But my recommendation is if you'd like to manage the mqtt client object with redux, then it should be created and destroyed in a redux action creator as well, such that React components don't have to access MQTT-related code directly at all. If you could provide at least your reducer and action creator, maybe I could try to help.

badcodercpp commented 5 years ago

While storing connected mqtt client to redux, its always a good idea to inject the client object to the redux store via middleware, and this feature is out of scope for this project as I guess. So I write a middleware to solve this issue, please have a look at this - https://www.npmjs.com/package/redux-mqtt This middleware works well with this package to store mqtt client.