Rajan Lagah
Rajan Lagah

Reputation: 2528

Not connecting user using MQTTjs on cloudmqtt

I am having this code in my file (belong to react)

const client = mqtt.connect({
  host: 'mqtt://m16.cloudmqtt.com',
  port: 1883,
  username: 'b*******k',
  password: 'gU******S',
});
client.on('connect', () => {
  console.log('hello');
  client.subscribe('v');
  client.publish('v', 'chal pa');
});
client.on('message', (topic, message) => {
  if (topic === 'v') {
    console.log('here my topic is v');
    // var connected = (message.toString() === 'true');
  }
  console.log('recived message from mqtt');
  console.log(message);
});
client.on('error', er => {
  console.log(er);
});


I am expecting to connect to mqtt broker and receive some message.
But nothing happened. When i check log file in cloudmqtt.com

enter image description here

I am stuck here can anybody help. Link to any blog/video that will help will be highly appreciated.

I am using mqttjs

Upvotes: 0

Views: 223

Answers (1)

hardillb
hardillb

Reputation: 59618

You have explicitly told the MQTTjs library to use native MQTT rather than MQTT over Websockets by using mqtt:// on the start of the URI.

If you want to use MQTT over websockets the URI should start with ws://

Secondly you are using port 1883, this is normally used for native MQTT not MQTT over websockets. The cloudmqtt docs suggest you should be using a port number that starts with a 3 to access the websockets listener.

Upvotes: 1

Related Questions