davesters / rn-native-mqtt

An MQTT client for React Native that actually works and exposes a simple Javascript interface
MIT License
64 stars 44 forks source link

Connect error #6

Open ghost opened 4 years ago

ghost commented 4 years ago

Hi, i'm trying to get simple test of this and i'm get error when trying to connect to server, i test my own mqtt server and free public server like mosquitto, but the result its same.

Im testing this on android

I'm start my project with npx react-native init MqttSample --template react-native-template-typescript

then i'm install the lib with: npm install react-native-native-mqtt --save

then i run react-native link react-native-native-mqtt

And my simple code is:

import * as Mqtt from 'react-native-native-mqtt';

const client = new Mqtt.Client('mqtt://test.mosquitto.org:1883');

client.connect(
      {
            clientId: 'miclienteyujuwao',
            cleanSession: true,
            keepAlive: 10,
            timeout: 30000,
            autoReconnect: true,
      },
      err => {
            alert('One error :(');
            console.warn(err); // Error: mqtt://test.mosquitto.org:1883
      },
);

Simply, i'm test with and without typescript templates and the result its same.

System:
    OS: Windows 10 Home, 1909, x64
    CPU: Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 16.0 GB
Packages:
    "react": "16.11.0",
    "react-native": "0.62.1",
    "react-native-native-mqtt": "^0.1.5"
basilbai commented 4 years ago

same question

jackdewhurst commented 4 years ago

Same, nothing happens on client.connect(...), no error, logs or anything, doesn't matter what options I try

panchal-krunal commented 4 years ago

Any solution on this ?

kvaghas commented 4 years ago

Works well on iOS, nothing at all on Android.

songzuyun commented 4 years ago

same question

songzuyun commented 4 years ago

Works well on iOS, nothing at all on Android.

Any solution on this now?

imetric commented 4 years ago

Change the the with tcp protocal const client = new Mqtt.Client('tcp://test.mosquitto.org:1883');

llaneiro commented 3 years ago

Change the the with tcp protocal const client = new Mqtt.Client('tcp://test.mosquitto.org:1883');

did not work for me, when using 'tcp' it returns undefined error

in the image the first log is using the 'mqtt' and in the second the 'tcp' image

clvs7-gh commented 3 years ago

I had same issue.

From Eclipse Paho's document ( https://www.eclipse.org/paho/files/javadoc/org/eclipse/paho/client/mqttv3/MqttConnectOptions.html#setUserName-java.lang.String- ) :

public void setUserName(java.lang.String userName) Sets the user name to use for the connection. Throws: java.lang.IllegalArgumentException - if the user name is blank or only contains whitespace characters.

So, I think below implementation of this library is a bit wrong:

https://github.com/davesters/rn-native-mqtt/blob/master/android/src/main/java/com/davesters/reactnative/mqtt/MqttClient.java#L66 https://github.com/davesters/rn-native-mqtt/blob/master/src/index.ts#L24

username must be specified. Can't be omitted.

SarathKannan commented 3 years ago

i changed the protocal and added the usename and is worked. const client = new Mqtt.Client('tcp://test.mosquitto.org:1883'); and client.connect({clientId: 'CLIENT_ID_0012656562645', username: 'USERNAME_0012656562645'}, err => {console.log(err)});

@imetric @clvs7-gh thanks a lot..