MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

[Browser] client on connect handler triggered continuously

Open goliatone opened this issue 5 years ago • 5 comments

I have a browser application with the following code:

const client = mqtt.connect(options);

    client.on('connect', e => {
        client.publish(`events/${clientId}/service/up`, JSON.stringify({
            action: 'up',
            client: clientId,
            timestamp: Math.floor(Date.now() / 1000)
        }));
    });

The connect handler seems to be called continuously:

{
  "cmd": "connack",
  "retain": false,
  "qos": 0,
  "dup": false,
  "length": 2,
  "topic": null,
  "payload": null,
  "sessionPresent": false,
  "returnCode": 0
}

Output from subscribing to events/#:

{"action":"up","client":"web-client-jv665k8a","timestamp":1556772528}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772530}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772533}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772536}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772539}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772542}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772545}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772548}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772551}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772554}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772557}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772560}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772563}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772566}
{"action":"up","client":"web-client-jv665k8a","timestamp":1556772568}

This only happens when the client is not busy sending/receiving messages.

What could explain this behavior?

goliatone avatar May 02 '19 04:05 goliatone

@goliatone Were you able to solve this issue? I am also facing this issue in browser.

abhinav-juneja avatar Jun 10 '19 07:06 abhinav-juneja

Tested ./example/client/simple-publish.js The connect handler seems to be called continuously:

Screenshot from 2019-06-10 17-41-43

abhinav-juneja avatar Jun 10 '19 12:06 abhinav-juneja

@abhinav-juneja the behavior seems to be intermittent, I have not been able to understand the issue behind

goliatone avatar Jul 23 '19 17:07 goliatone

I have the same problem

Gianlo98 avatar Jan 27 '20 20:01 Gianlo98

Did anyone manage to solve it? I'm using React and every time I make a client.publish() it adds an extra connection to the broker.

chacalgbi avatar Aug 01 '22 01:08 chacalgbi

Try using mqttv5 and feel free to reopen this issue if the problem persists

robertsLando avatar Jul 31 '23 15:07 robertsLando