5

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.

user8951490
  • 833
  • 1
  • 10
  • 21

2 Answers2

6

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

Matt Ball
  • 414
  • 3
  • 9
0

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

yarn add react-native-paho-mqtt

Adriaan Marain
  • 294
  • 3
  • 10
  • I already did. Unfortunately, everytime I enter my server's IP, it would give me a URI error. – user8951490 Apr 09 '18 at 12:20
  • Can you copy+paste the error and the line that's causing the error? – Adriaan Marain Apr 09 '18 at 12:22
  • Updated... when I append the uri with ws:// the uri error goes away... But I cannot establish a connection then... I would say that my MQTT server doesn't support WebSockets, but I have used the python version of paho without any problems--with WebSockets... Now the problem is that I just get a "onConnectionLost. undefined" object in my console. – user8951490 Apr 09 '18 at 12:32