admin管理员组文章数量:1312658
I recently downloaded paho-mqtt
via 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-mqtt
via 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://
ormqtt://
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
2 Answers
Reset to default 8The 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
版权声明:本文标题:javascript - Paho MQTT: Possible importing error? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741913679a2404594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论