admin管理员组

文章数量:1312658

I recently downloaded paho-mqttvia yarn. The problem is I am not sure if I am importing it correctly, because I am getting an error:

Cannot read property 'Client' of undefined

The way I am importing and using it is like this:

import Paho from 'paho-mqtt'; 
var client = new Paho.MQTT.Client(location.host, location.port, location.clientID)

const MQTTConnectAndMessage = (message) => {
    client.connect({onSuccess: sendMQTTMessage})
}

const sendMQTTMessage = (msg) => {
    let message = new Paho.MQTT.Message(msg); 
    message.destinationName = location.messageDestination; 
    client.send(message); 
}

location.host = a string for the IP

location.port = a number for the port

location.clientID = a string for the clientID

If it is relevant, I am attempting to use it within a React Native app.

Maybe this module is not meant to be downloaded via NPM or Yarn? Or maybe I am not supposed to be importing "Paho"?

EDIT: when using react-native-paho-mqtt--this is the code I am using:

const client = new Client({ uri: 'ws://myiphere/ws', port: 1883, clientId: 'clientId', storage: myStorage});

const sendMQTTMessage = (msg) => {
    client.on('connectionLost', (responseObject) => {
        if (responseObject.errorCode !== 0) {
          console.log("connection lost!");
        }
      });
      client.on('messageReceived', (message) => {
        console.log(message.payloadString);
      });

    client.connect()
        .then(() => {
            const message = new Message(msg);
            message.destinationName = 'F2/BOX2/LED3';
            client.send(message);
        })
        .catch((responseObject) => {
            if (responseObject.errorCode !== 0) {
             console.log('onConnectionLost:' + responseObject.errorMessage);
            }
        });
} 

export {
    sendMQTTMessage
}

I notice that whenever I enter anything that isn't prefaced with ws:// (web sockets) I would get a URI error.

I recently downloaded paho-mqttvia yarn. The problem is I am not sure if I am importing it correctly, because I am getting an error:

Cannot read property 'Client' of undefined

The way I am importing and using it is like this:

import Paho from 'paho-mqtt'; 
var client = new Paho.MQTT.Client(location.host, location.port, location.clientID)

const MQTTConnectAndMessage = (message) => {
    client.connect({onSuccess: sendMQTTMessage})
}

const sendMQTTMessage = (msg) => {
    let message = new Paho.MQTT.Message(msg); 
    message.destinationName = location.messageDestination; 
    client.send(message); 
}

location.host = a string for the IP

location.port = a number for the port

location.clientID = a string for the clientID

If it is relevant, I am attempting to use it within a React Native app.

Maybe this module is not meant to be downloaded via NPM or Yarn? Or maybe I am not supposed to be importing "Paho"?

EDIT: when using react-native-paho-mqtt--this is the code I am using:

const client = new Client({ uri: 'ws://myiphere/ws', port: 1883, clientId: 'clientId', storage: myStorage});

const sendMQTTMessage = (msg) => {
    client.on('connectionLost', (responseObject) => {
        if (responseObject.errorCode !== 0) {
          console.log("connection lost!");
        }
      });
      client.on('messageReceived', (message) => {
        console.log(message.payloadString);
      });

    client.connect()
        .then(() => {
            const message = new Message(msg);
            message.destinationName = 'F2/BOX2/LED3';
            client.send(message);
        })
        .catch((responseObject) => {
            if (responseObject.errorCode !== 0) {
             console.log('onConnectionLost:' + responseObject.errorMessage);
            }
        });
} 

export {
    sendMQTTMessage
}

I notice that whenever I enter anything that isn't prefaced with ws:// (web sockets) I would get a URI error.

Share Improve this question edited Apr 9, 2018 at 12:30 user8951490 asked Apr 9, 2018 at 10:14 user8951490user8951490 8431 gold badge11 silver badges21 bronze badges 4
  • A semicolon is missing in the 2nd line – Alexander Farber Commented Apr 9, 2018 at 11:09
  • Still prints the same error message... – user8951490 Commented Apr 9, 2018 at 11:33
  • You have to use ws:// urls with the paho javascript client, it does not support native MQTT (tcp:// or mqtt:// urls). You will need to ensure your MQTT broker supports MQTT over Websockets and ensure it is enabled – hardillb Commented Apr 9, 2018 at 12:32
  • This will also most likely not be on port 1883 (default native MQTT port) – hardillb Commented Apr 9, 2018 at 12:34
Add a ment  | 

2 Answers 2

Reset to default 8

The paho-mqtt library has changed, and the example code is incorrect

var client = new Paho.MQTT.Client(location.host, location.port, location.clientID)

Should be changed to (remove MQTT from Object path):

var client = new Paho.Client(location.host, location.port, location.clientID)

See the "breaking changes" in the GitHub README page: paho.mqtt.javascript

Try this react-native patible library: https://www.npmjs./package/react-native-paho-mqtt

yarn add react-native-paho-mqtt

本文标签: javascriptPaho MQTT Possible importing errorStack Overflow