taoqf / MQTT.js

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

Unable to connect / React native MQTT #9

Open kindthr opened 2 years ago

kindthr commented 2 years ago

Hey! I'm trying to connect and get the data from topic sent by sensor. Unfortunately the connection not working. Any help is appreciated.

import init from 'react_native_mqtt'
import AsyncStorage from '@react-native-async-storage/async-storage'

 init({
  size: 10000,
  storageBackend: AsyncStorage,
  defaultExpires: 1000 * 3600 * 24,
  enableCache: true,
  reconnect: true,
  sync : {
  }
});

const host = '52.11.11.11'
const port = '8883'
const connectUrl = `mqtt://${host}:${port}`
clientID = "clientID-" + parseInt(Math.random() * 100);
export default class TestScreen extends Component {

  constructor(){
    super();
    this.onConnectionLost = this.onConnectionLost.bind(this)
    this.onConnect = this.onConnect.bind(this)
    const client = new Paho.MQTT.Client(host, Number(port), clientID);
    client.onConnectionLost = this.onConnectionLost;
    client.connect({ 
      onSuccess: this.onConnect,
      useSSL: true,
      userName: 'admin',
      password: 'admin',
      onFailure: (e) => {console.log("here is the error" , e); }

    });

    this.state = {
      message: [''],
      client,
      messageToSend:'',
      isConnected: false,
    };
  }
  onConnect = () => {
   // const { client } = this.state;
    console.log("Connected!!!!");
    //client.subscribe('hello/world');
    this.setState({isConnected: true, error: ''})
  };

It log : here is the error Object { "errorCode": 7, "errorMessage": "AMQJS0007E Socket error:undefined.", "invocationContext": undefined, }

hardillb commented 2 years ago

"Answered" https://iot.stackexchange.com/questions/6068/make-connection-between-react-native-raspberry-mqtt