Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Client continuously connects, gets disconnected, reconnects #1913

Open
QuentinFarizon opened this issue Jul 29, 2024 · 1 comment
Open
Labels

Comments

@QuentinFarizon
Copy link

QuentinFarizon commented Jul 29, 2024

MQTTjs Version

5.9.0

Broker

Mosquitto (from Stackhero)

Environment

NodeJS

Description

When I connect to the broker, the connection gets closed very quickly after established.
I can chain one or two publish or subscribe immediately after connection success, but 500ms - 1s later it gets closed.

Minimal Reproduction

return mqtt.connect(<STACKHERO_MOSQUITTO_URL_TLS>, {
auth: <...>
})

Debug logs

  mqttjs:client MqttClient :: version: 5.9.0 +0ms
  mqttjs:client MqttClient :: environment node +0ms
  mqttjs:client MqttClient :: options.protocol mqtts +0ms
  mqttjs:client MqttClient :: options.protocolVersion 4 +0ms
  mqttjs:client MqttClient :: options.username admin +0ms
  mqttjs:client MqttClient :: options.keepalive 60 +0ms
  mqttjs:client MqttClient :: options.reconnectPeriod 1000 +0ms
  mqttjs:client MqttClient :: options.rejectUnauthorized undefined +0ms
  mqttjs:client MqttClient :: options.properties.topicAliasMaximum undefined +0ms
  mqttjs:client MqttClient :: clientId mqttjs_4fdb8f15 +0ms
  mqttjs:client MqttClient :: setting up stream +0ms
  mqttjs:client connect :: calling method to clear reconnect +1ms
  mqttjs:client _clearReconnect : clearing reconnect timer +0ms
  mqttjs:client connect :: using streamBuilder provided to client to create stream +0ms
  mqttjs:client connect :: pipe stream to writable stream +22ms
  mqttjs:client connect: sending packet `connect` +0ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'mqttjs_4fdb8f15',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: 'admin',
  mqttjs:client   password: '<my-password>',
  mqttjs:client   properties: undefined
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +1ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +5ms
(node:1534689) [SEQUELIZE0018] DeprecationWarning: The "field" option in attribute definitions has been renamed to "columnName".
(Use `node --trace-deprecation ...` to show where the warning was created)
  mqttjs:client writable stream :: parsing buffer +38ms
  mqttjs:client parser :: on packet push to packets array. +0ms
  mqttjs:client work :: getting next packet in queue +0ms
  mqttjs:client work :: packet pulled from queue +0ms
  mqttjs:client _handlePacket :: emitting packetreceive +0ms
  mqttjs:client _handleConnack +1ms
  mqttjs:client _setupKeepaliveManager :: keepalive 60 (seconds) +0ms
  mqttjs:client KeepaliveManager: set keepalive to 60000ms +0ms
  mqttjs:client connect :: sending queued packets +1ms
  mqttjs:client deliver :: entry undefined +0ms
  mqttjs:client _resubscribe +0ms
  mqttjs:client (mqttjs_4fdb8f15)stream :: on close +1s
  mqttjs:client _flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function +0ms
  mqttjs:client stream: emit close to MqttClient +0ms
  mqttjs:client close :: connected set to `false` +0ms
  mqttjs:client close :: clearing connackTimer +0ms
  mqttjs:client _destroyKeepaliveManager :: destroying keepalive manager +0ms
  mqttjs:client close :: calling _setupReconnect +0ms
  mqttjs:client _setupReconnect :: emit `offline` state +0ms
MQTT offline
  mqttjs:client _setupReconnect :: set `reconnecting` to `true` +1ms
  mqttjs:client _setupReconnect :: setting reconnectTimer for 1000 ms +0ms
  mqttjs:client reconnectTimer :: reconnect triggered! +1s
  mqttjs:client _reconnect: emitting reconnect to client +1ms
MQTT broker reconnected
  mqttjs:client _reconnect: calling connect +0ms
  mqttjs:client connect :: calling method to clear reconnect +0ms
  mqttjs:client _clearReconnect : clearing reconnect timer +0ms
  mqttjs:client connect :: using streamBuilder provided to client to create stream +0ms
  mqttjs:client connect :: pipe stream to writable stream +2ms
  mqttjs:client connect: sending packet `connect` +0ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'mqttjs_4fdb8f15',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: 'admin',
  mqttjs:client   password: '<my-password>',
  mqttjs:client   properties: undefined
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +0ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +1ms
  mqttjs:client writable stream :: parsing buffer +34ms
  mqttjs:client parser :: on packet push to packets array. +1ms
  mqttjs:client work :: getting next packet in queue +0ms
  mqttjs:client work :: packet pulled from queue +0ms
  mqttjs:client _handlePacket :: emitting packetreceive +0ms
  mqttjs:client _handleConnack +0ms
  mqttjs:client _setupKeepaliveManager :: keepalive 60 (seconds) +0ms
  mqttjs:client KeepaliveManager: set keepalive to 60000ms +0ms
  mqttjs:client connect :: sending queued packets +0ms
  mqttjs:client deliver :: entry undefined +0ms
  mqttjs:client _resubscribe +0ms
  mqttjs:client (mqttjs_4fdb8f15)stream :: on close +1s
  mqttjs:client _flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function +0ms
  mqttjs:client stream: emit close to MqttClient +0ms
  mqttjs:client close :: connected set to `false` +0ms
  mqttjs:client close :: clearing connackTimer +0ms
  mqttjs:client _destroyKeepaliveManager :: destroying keepalive manager +0ms
  mqttjs:client close :: calling _setupReconnect +0ms
  mqttjs:client _setupReconnect :: emit `offline` state +0ms
MQTT offline
  mqttjs:client _setupReconnect :: set `reconnecting` to `true` +1ms
  mqttjs:client _setupReconnect :: setting reconnectTimer for 1000 ms +0ms
  mqttjs:client reconnectTimer :: reconnect triggered! +1s
  mqttjs:client _reconnect: emitting reconnect to client +0ms
MQTT broker reconnected
  mqttjs:client _reconnect: calling connect +0ms
  mqttjs:client connect :: calling method to clear reconnect +0ms
  mqttjs:client _clearReconnect : clearing reconnect timer +0ms
  mqttjs:client connect :: using streamBuilder provided to client to create stream +1ms
  mqttjs:client connect :: pipe stream to writable stream +1ms
  mqttjs:client connect: sending packet `connect` +0ms
  mqttjs:client _writePacket :: packet: {
  mqttjs:client   cmd: 'connect',
  mqttjs:client   protocolId: 'MQTT',
  mqttjs:client   protocolVersion: 4,
  mqttjs:client   clean: true,
  mqttjs:client   clientId: 'mqttjs_4fdb8f15',
  mqttjs:client   keepalive: 60,
  mqttjs:client   username: 'admin',
  mqttjs:client   password: '<my-password>',
  mqttjs:client   properties: undefined
  mqttjs:client } +0ms
  mqttjs:client _writePacket :: emitting `packetsend` +0ms
  mqttjs:client _writePacket :: writing to stream +0ms
  mqttjs:client _writePacket :: writeToStream result true +1ms
  mqttjs:client writable stream :: parsing buffer +44ms
  mqttjs:client parser :: on packet push to packets array. +0ms
  mqttjs:client work :: getting next packet in queue +0ms
  mqttjs:client work :: packet pulled from queue +0ms
  mqttjs:client _handlePacket :: emitting packetreceive +0ms
  mqttjs:client _handleConnack +0ms
  mqttjs:client _setupKeepaliveManager :: keepalive 60 (seconds) +0ms
  mqttjs:client KeepaliveManager: set keepalive to 60000ms +0ms
  mqttjs:client connect :: sending queued packets +1ms
  mqttjs:client deliver :: entry undefined +0ms
  mqttjs:client _resubscribe +0ms
  mqttjs:client (mqttjs_4fdb8f15)stream :: on close +1s
  mqttjs:client _flushVolatile :: deleting volatile messages from the queue and setting their callbacks as error function +0ms
  mqttjs:client stream: emit close to MqttClient +0ms
  mqttjs:client close :: connected set to `false` +0ms
  mqttjs:client close :: clearing connackTimer +0ms
  mqttjs:client _destroyKeepaliveManager :: destroying keepalive manager +0ms
  mqttjs:client close :: calling _setupReconnect +0ms
  mqttjs:client _setupReconnect :: emit `offline` state +0ms
MQTT offline
  mqttjs:client _setupReconnect :: set `reconnecting` to `true` +0ms
  mqttjs:client _setupReconnect :: setting reconnectTimer for 1000 ms +0ms

@robertsLando
Copy link
Member

The reason why the connection is closed is not clear looking at the logs, I just see the stream is closed after 1s receiving the connack.

mqttjs:client (mqttjs_4fdb8f15)stream :: on close +1s

This makes me think it's the broker closing the connection for some reason (like another client connecting with the same clientId). I suggest to check your logs on broker side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants