VictorHAS / mqtt-react-hooks

ReactJS library for Pub/Sub communication with an MQTT broker using Hooks
MIT License
91 stars 28 forks source link

MQTT attempts to reconnect every few ms #36

Open MartinMuzatko opened 2 years ago

MartinMuzatko commented 2 years ago

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

KevynKelso commented 2 years ago

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, }}

... `

Rainase commented 1 year ago

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