eclipse-paho / eclipse-paho/paho.mqtt.javascript

Got [Error: AMQJS0011E Invalid state already connected.] when change the Screen in react native app.

Open
#250 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.2k
Forks
466
PR merge metrics
No merged PRs in 30d

Description

Hi,

env: "react-native": "0.69.6",
version: "paho-mqtt": "^1.1.0",

I wrote the code like this :

```js
function onMessage(message) {
let data = JSON.parse(message.payloadString)
console.log(data)
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log('onConnectionLost:' + responseObject.errorMessage)
}
}

useEffect(() => {
if (something) {
try {
client.connect({
onSuccess: () => {
console.log('Connected!')
client.subscribe(`device/default`)
client.onConnectionLost = onConnectionLost
client.onMessageArrived = onMessage
},
onFailure: () => {
console.log('Failed to connect!')
},
})
} catch (error) {
console.log(error)
}
}
}, [something])

```
It works but when I change the screen(react-navigation) I will got this error:
AMQJS0011E Invalid state already connected.
and nothing gets.

( Only when I reload the expo can get data again.)

Is there anything I miss?

Thank you!

![image](https://user-images.githubusercontent.com/61406208/198515285-319edc95-4380-4270-a46d-02c34257e23d.png)

Contributor guide

Open the contributing guide

Research direction

Start with the useEffect and client.connect flow shown in the issue, then trace how the React Navigation screen change affects the existing MQTT client. Determine the lifecycle behavior needed to avoid the invalid already-connected state, and verify that navigating between screens preserves message reception without requiring an Expo reload.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react-native
Domain
mobile, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.