taoqf / MQTT.js

The MQTT client for Node.js and the browser
Other
12 stars 8 forks source link

Unable to connect - MQTT protocol URL (plugin@taoqf/react-native-mqtt) #8

Open shalini-whiz opened 2 years ago

shalini-whiz commented 2 years ago

Here, I have integrated this plugin in my react native project and trying to connect URL with protocol mqtt. Referred doc https://github.com/taoqf/MQTT.js/blob/react-native/README.md Am unable to detect either connected or not. Below is my snippet let mUrl = "mqtt://xxxx.xxxx.in"; let mOptions = { username: server_user, password: serverpass, protocol: 'mqtt', clientId: 'mqttjs' + Math.random().toString(16).substr(2, 8), rejectUnauthorized: false, protocolId: 'mqtt', defaultProtocol:"mqtt", protocolVersion: 4, clean:false } var client = mqtt.connect(mUrl,mOptions); client.on('connect', function () { console.log("connected"); }) }) client.on('error', function ( err) { console.log("client err : " + err) })

Can anyone help ?

Thanks

galihariad commented 2 years ago

Same, I am new to react-native so still figuring out how to test this.. I tried to add [this code](https://github.com/taoqf/MQTT.js/tree/react-native#:~:text=var%20mqtt%20%3D%20require,toString()) to my button component, but the console.log I put inside the client.on('connect', function () { function hasn't come out yet as I checked in the browser console :/

cc: @taoqf

DzungDH35 commented 2 years ago

Are there any updates on this issue? I just cannot connect to the broker, moreover, no errors are fired

shalini-whiz commented 2 years ago

@galihariad @DzungDH35 I switched to https://github.com/SudoPlz/sp-react-native-mqtt

DzungDH35 commented 2 years ago

@shalini-whiz I used to try the lib you mentioned before, but there are some problems with connect() function which fire an exception. The console log is like below: image

I do not understand why it is an UnknownHostException, I try to test this broker with a MQTTClient app and it still works

shalini-whiz commented 2 years ago

@DzungDH35 , for your reference added snippet. If you find issue still, raise issue in respective plugin itself let options = { uri: "mqtt" + "://XXXX.XXXX.XX:PORT", user: "", pass: "", auth: true, keepalive: 32000 } options.clientId = "clientId"+Date.now() MQTT.createClient(options).then((client) => { client.connect();

  client.on('closed', () => {
    console.log('mqtt.event.closed');
  });

  client.on('error', (msg) => {
    console.log('bin mqtt.event.error', msg);
  });

  client.on('message', (msg) => {
    console.log('mqtt.event.message', msg);
  });

  client.on('connect', () => {
    console.log('connected');
    client.subscribe(topic_name, 2)
  });

}).catch((err) => {
  console.log("err : " + err);
});
DzungDH35 commented 2 years ago

@shalini-whiz thank you so much. I still have no idea why your codes work :), my flow is quite the same as yours except for false value of auth, though ... whatever, it works, many thanks :)

QMY-ZQ commented 1 year ago

@shalini-whiz 但是用sp_react_native_mqtt对于android会友好,对于ios就会有点不兼容