mqtt-react-hooks
mqtt-react-hooks copied to clipboard
MQTT attempts to reconnect every few ms
Hello! I am using
"mqtt-react-hooks": "^2.0.3",
"next": "11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2"
In my setup, the Connector connects to ws://test.mosquitto.org:8080. I tried various other servers to verify this problem persists.
Even without a useSubscription hook, the module attempts to connect every few milliseconds.

Even though I get 101 switching protocols.
Interestingly, I also only get Status: undefined
This is the code:
import { Connector, useMqttState } from 'mqtt-react-hooks'
const Home: NextPage = () => {
const { connectionStatus } = useMqttState()
return (
<div>
<Connector brokerUrl="ws://test.mosquitto.org:8080">
{ `Status: ${connectionStatus}` }
</Connector>
</div>
)
}
export default Home
I added a codesandbox where I can also observe the problem: https://codesandbox.io/s/headless-water-8hfcv
I'm having the exact same issue with a ws scheme versus a wss scheme. wss works fine for me, but the library seems to reject ws connections.
-Edit: So I think the problem for me was my broker being setup with authentication and access control settings, but not providing the authentication when connecting from react. I also had to set keepalive to a value > 0.
Here is an example of my configuration:
<Connector brokerUrl={'tcp://xx.xxx.xx.xx:xxxx'} options={{ keepalive: 120, }} > ... </Connector>
i have the same reconnect happening every second with nextJS v12. is there a proper solution for this yet ?