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

Connection refused: Bad username or password

Open nicoworq opened this issue 4 years ago • 6 comments

Hi! I'm trying to connect to a Thingstream broker running MQTT 3.1.1, using user and pass.

  var username = "user";
  var pass = "pass";
 
  var client = mqtt.connect('mqtt:mqtt.thingstream.io',
    {   
      username: username,
      password: pass,
    }
  )

But I'm getting this error:

Error: Connection refused: Bad username or password
    at MqttClient._handleConnack (\mqttNode\node_modules\mqtt\lib\client.js:1190:15)
    at MqttClient._handlePacket (\mqttNode\node_modules\mqtt\lib\client.js:424:12)
    at work (\mqttNode\node_modules\mqtt\lib\client.js:321:12)
    at Writable.writable._write (\mqttNode\node_modules\mqtt\lib\client.js:335:5)
    at doWrite (\mqttNode\node_modules\readable-stream\lib\_stream_writable.js:409:139)
    at writeOrBuffer (\mqttNode\node_modules\readable-stream\lib\_stream_writable.js:398:5)
    at Writable.write (\mqttNode\node_modules\readable-stream\lib\_stream_writable.js:307:11)
    at Socket.ondata (internal/streams/readable.js:731:22)
    at Socket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:293:12) {
  code: 4

I'm able to connect without problems using mosquitto client.

Any suggestion? Thanks in advance!

nicoworq avatar Oct 22 '21 21:10 nicoworq

Are the credentials just wrong?

Get Outlook for iOShttps://aka.ms/o0ukef


From: Nicolas @.> Sent: Friday, October 22, 2021 4:35:45 PM To: mqttjs/MQTT.js @.> Cc: Subscribed @.***> Subject: [mqttjs/MQTT.js] Connection refused: Bad username or password (Issue #1345)

Hi! I'm trying to connect to a Thingstream broker running MQTT 3.1.1, using user and pass.

var username = "user"; var pass = "pass";

var client = mqtt.connect('mqtt:mqtt.thingstream.io', { username: username, password: pass, } )

But I'm getting this error:

Error: Connection refused: Bad username or password at MqttClient._handleConnack (\mqttNode\node_modules\mqtt\lib\client.js:1190:15) at MqttClient._handlePacket (\mqttNode\node_modules\mqtt\lib\client.js:424:12) at work (\mqttNode\node_modules\mqtt\lib\client.js:321:12) at Writable.writable._write (\mqttNode\node_modules\mqtt\lib\client.js:335:5) at doWrite (\mqttNode\node_modules\readable-stream\lib_stream_writable.js:409:139) at writeOrBuffer (\mqttNode\node_modules\readable-stream\lib_stream_writable.js:398:5) at Writable.write (\mqttNode\node_modules\readable-stream\lib_stream_writable.js:307:11) at Socket.ondata (internal/streams/readable.js:731:22) at Socket.emit (events.js:400:28) at addChunk (internal/streams/readable.js:293:12) { code: 4

Any suggestion? Thanks in advance!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmqttjs%2FMQTT.js%2Fissues%2F1345&data=04%7C01%7Cyoseph.maguire%40microsoft.com%7C5e006c7fd55542b8f5a208d995a3e8ff%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637705353483246087%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=y%2F8oaEUEoO3V4UEwBCfaipjPiA5UQWW1C4r4c4gcR3o%3D&reserved=0, or unsubscribehttps://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAB6VMLG52RQJSLVQU3YD2KDUIHKLDANCNFSM5GRNTBSA&data=04%7C01%7Cyoseph.maguire%40microsoft.com%7C5e006c7fd55542b8f5a208d995a3e8ff%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637705353483256079%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=4aZPbrMkhiYaFiTvpW7fE%2FhdgKhEfrT4dNBRXSDm71w%3D&reserved=0.

YoDaMa avatar Oct 22 '21 21:10 YoDaMa

Nop, I'm able to connect without problems using mosquitto client.

nicoworq avatar Oct 23 '21 00:10 nicoworq

Is that the correct way to specify a user and password? Thanks!

nicoworq avatar Oct 26 '21 14:10 nicoworq

Probably because mosquitto_pub and mosquitto_sub bypass authentication on mosquitto. As i remember, there should be a root user called mosquitto that is always authorised on the mosquitto tools.

msalafia avatar Nov 24 '21 16:11 msalafia

Is there a solution for this issue? I'm getting the same error connecting to RabbitMQ. I have 2 instances of the server, The first connected successfully but the second got this error. How can I fix this?

const client = mqtt.connect(mqtt://hostname:1883, {
      username: "username",
      password: "password",
    });

odeliat avatar Dec 16 '21 07:12 odeliat

when i put username and password mqtt does not work anymore

|camera              | TypeError: Cannot read properties of undefined (reading 'resubscribe')
0|camera              |     at MqttClient.subscribe (/home/Shinobi/node_modules/mqtt/lib/client.js:701:27)
0|camera              |     at MqttClient.<anonymous> (/home/Shinobi/libs/notifications/mqtt.js:45:28)
0|camera              |     at MqttClient.emit (node:events:539:35)
0|camera              |     at Readable.<anonymous> (/home/Shinobi/node_modules/mqtt/lib/client.js:1863:14)
0|camera              |     at Readable.emit (node:events:527:28)
0|camera              |     at endReadableNT (/home/Shinobi/node_modules/readable-stream/lib/_stream_readable.js:1094:12)
0|camera              |     at processTicksAndRejections (node:internal/process/task_queues:83:21)

using command line client works fine with username and password

moeiscool avatar Jul 21 '22 16:07 moeiscool

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

robertsLando avatar Jul 21 '23 14:07 robertsLando

MQTT 5.0.0 BETA is now available! Try it out and give us feedback: npm i mqtt@beta. It may fix your issues

I used the version 5.3.5 also have same issue.

frank-niu-github avatar Feb 01 '24 08:02 frank-niu-github

Can you connect using another client with same username/password?

robertsLando avatar Feb 01 '24 08:02 robertsLando

Can you connect using another client with same username/password?

Yes. I successfully used the MQTTX tool and same username/password to connect to our MQTT server.

My vue3 source code as below:

image

frank-niu-github avatar Feb 02 '24 00:02 frank-niu-github

do you have a way to see what's coming to the broker? I would be curious to know what broker receives, I think it could be code/decode issue with the password/username, does they contain some strange char/symbol?

robertsLando avatar Feb 02 '24 07:02 robertsLando

do you have a way to see what's coming to the broker? I would be curious to know what broker receives, I think it could be code/decode issue with the password/username, does they contain some strange char/symbol?

Hi RobertsLando, Thanks for you reply. Actually the username/password have ‘-’ symbol between the string word. How can I fixed this decode issue?

frank-niu-github avatar Feb 02 '24 07:02 frank-niu-github

Mine was just a supposition, the - doesn't have any decode issue. I suggest you to try checking what the broker receives

robertsLando avatar Feb 02 '24 07:02 robertsLando

Mine was just a supposition, the - doesn't have any decode issue. I suggest you to try checking what the broker receives

Thanks for your suggestions. This is my fault. Because our MQTT broker setting the HTTP auth. The backend of the cloud server is responsible for verifying the username/password send by Websocket. Always return bad username/password when there is no response from the backend.

frank-niu-github avatar Feb 02 '24 08:02 frank-niu-github

Closing this for now as I have a feel that when this happens it's most likely a user issue

robertsLando avatar Feb 02 '24 09:02 robertsLando