mqtt-react-hooks icon indicating copy to clipboard operation
mqtt-react-hooks copied to clipboard

MQTT attempts to reconnect every few ms

Open MartinMuzatko opened this issue 4 years ago • 2 comments

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.

image

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

MartinMuzatko avatar Oct 15 '21 21:10 MartinMuzatko

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>

KevynKelso avatar Jan 25 '22 19:01 KevynKelso

i have the same reconnect happening every second with nextJS v12. is there a proper solution for this yet ?

Rainase avatar Sep 20 '22 17:09 Rainase