You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reconnect interval is not a valid option. To reconnect the recommendations from other issues are use one but not both of:
Call client.connect inside onConnectionLost
set reconnect : true
Anyway these two will only reconnect once or twice but if your broker is down for longer your app may not reconnect. Try this yourself.
If you want something to persistently try to reconnect in react you need to code it yourself carefully, without methods 1 or 2 described above as these conflict with custom reconnect logic. Something like this worked for me:
Someting to think about is not put render changing callbacks on the success or error callbacks as these can't be removed unlike the other callbacks assigned by mutation
consthealthCheckSeconds=10;constSomeComponent=()=>{const[client]=React.useState(newClient("localhost",9001,"random-user"));constconnectClient=React.useCallback(()=>{client.onMessageArrived=myCallback;client.connect({userName: "username",password: "username",})},[client])React.useEffect(()=>{letcontinousFailures=0;connectClient();consthealthCheck=setInterval(()=>{// if you have been offline for some time reconnectif(continousFailures===10){connectClient();continousFailures=0;return;}// if your client is connected reset the count of continous failuresif(client.isConnected()){continousFailures=0;}else{continousFailures++;}},healthCheckSeconds*1000)return()=>{clearInterval(healthCheck);// do some cleanup of all your callbacks or they may trigger unwanted events or rerenders if they change stateclient.onMessageArrived=()=>{}if(client.isConnected()){client.disconnect()}}},[client])return<div>helloworld</div>}
I'm trying to reconnect after lost connection using javascript (React native)
The text was updated successfully, but these errors were encountered: