davesters / rn-native-mqtt Goto Github PK
View Code? Open in Web Editor NEWAn MQTT client for React Native that actually works and exposes a simple Javascript interface
License: MIT License
An MQTT client for React Native that actually works and exposes a simple Javascript interface
License: MIT License
I just installed react-native-native-mqtt and it works fine on android but as I do pod install on ios it fired me this error
[!] CocoaPods could not find compatible versions for pod "react-native-native-mqtt":
运行
client.close()
报错:client not disconnected
Hello!
I have tried using the module while using react-native-cli. After some time I managed to get it to work. I have however now tried to use it using Expo, and I keep getting the same error:
I have the following code:
import React, {useEffect, useState} from 'react'
import { View, Button, Text} from 'react-native';
import * as Mqtt from 'react-native-native-mqtt';
import buffer from 'buffer'
global.Buffer = buffer.Buffer;
const Mqtt = ({client}) => {
const [error, setError] = useState('');
const [payload, setPayload] = useState('');
const [connectStatus, setConnectStatus] = useState('Disconnected');
const mqttConnect = () => {
setConnectStatus("connecting");expo
client.connect({
clientId: "client",
username: "crel",
password: "pass",
cleanSession: true,
}, err => {setError(err)});
};
const mqttDisconnect = () => {
setConnectStatus("disconnecting");
client.disconnect();
};
useEffect(() => {
if(client != null)
{
client.on(Mqtt.Event.Message, (topic, message) => {
setPayload(message.toString());
});
client.on(Mqtt.Event.Connect, () => {
client.subscribe(["robot/+"], [0]);
setConnectStatus('connected');
});
client.on(Mqtt.Event.Error, (error) => {
console.error('error', (error));
setError(error);
});
client.on(Mqtt.Event.Disconnect, (cause) => {
setConnectStatus('Disconnected');
});
}
}, [client]);
const publishMessage = (str) => {
if(client != null && connectStatus == 'connected'){
const buf = Buffer.from(str);
client.publish("robot/test", buf, 0, false);
}
};
return (
<View>
<Text>{connectStatus}</Text>
<Text>error: {error}</Text>
<Button
onPress={mqttConnect}
title = "connect"
/>
<Button
onPress={mqttDisconnect}
title = "disconnect"
/>
<Button
onPress={
publishMessage
}
title = "publish test message"
/>
<Text>{payload}</Text>
</View>
)
}
export default Mqtt;
Does anyone have a tip, or experienced similar things?
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"
After following steps for adding bridge file and updating minimum deployment to 10.0 for iOS. White trying to run project I am getting following error:
React-native: "0.62.2"
Undefined symbols for architecture x86_64:
"nominal type descriptor for (extension in Foundation):__C.NSURLSessionWebSocketTask.Message", referenced from:
_symbolic _____y___________pG s6ResultO So25NSURLSessionWebSocketTaskC10FoundationE7MessageO s5ErrorP in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
_symbolic _____y___________pGz_Xx s6ResultO So25NSURLSessionWebSocketTaskC10FoundationE7MessageO s5ErrorP in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
"enum case for (extension in Foundation):__C.NSURLSessionWebSocketTask.Message.data((extension in Foundation):__C.NSURLSessionWebSocketTask.Message.Type) -> (Foundation.Data) -> (extension in Foundation):__C.NSURLSessionWebSocketTask.Message", referenced from:
react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.write(data: Foundation.Data, handler: (Swift.Error?) -> ()) -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
closure #1 () -> () in closure #1 (Swift.Result<(extension in Foundation):__C.NSURLSessionWebSocketTask.Message, Swift.Error>) -> () in closure #1 () -> () in react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.scheduleRead() -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
"enum case for (extension in Foundation):__C.NSURLSessionWebSocketTask.Message.string((extension in Foundation):__C.NSURLSessionWebSocketTask.Message.Type) -> (Swift.String) -> (extension in Foundation):__C.NSURLSessionWebSocketTask.Message", referenced from:
closure #1 () -> () in closure #1 (Swift.Result<(extension in Foundation):__C.NSURLSessionWebSocketTask.Message, Swift.Error>) -> () in closure #1 () -> () in react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.scheduleRead() -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
"(extension in Foundation):__C.NSURLSessionWebSocketTask.receive(completionHandler: (Swift.Result<(extension in Foundation):__C.NSURLSessionWebSocketTask.Message, Swift.Error>) -> ()) -> ()", referenced from:
closure #1 () -> () in react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.scheduleRead() -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
"type metadata accessor for (extension in Foundation):__C.NSURLSessionWebSocketTask.Message", referenced from:
react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.write(data: Foundation.Data, handler: (Swift.Error?) -> ()) -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
closure #1 () -> () in closure #1 (Swift.Result<(extension in Foundation):__C.NSURLSessionWebSocketTask.Message, Swift.Error>) -> () in closure #1 () -> () in react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.scheduleRead() -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
outlined init with copy of Swift.Result<(extension in Foundation):__C.NSURLSessionWebSocketTask.Message, Swift.Error> in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
"(extension in Foundation):__C.NSURLSessionWebSocketTask.send(_: (extension in Foundation):__C.NSURLSessionWebSocketTask.Message, completionHandler: (Swift.Error?) -> ()) -> ()", referenced from:
react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.write(data: Foundation.Data, handler: (Swift.Error?) -> ()) -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
ld: symbol(s) not found for architecture x86_64
Hi, just installed react-native-mqtt and first thing after doing it, is an error
"<MY_APP_PATH>/node_modules/react-native-mqtt/android/build/intermediates/library_manifest/debug/AndroidManifest.xml:16:5-17:15: AAPT: error: unexpected element service found in manifest."
.
Steps to reproduce:
It's freshly initialized app, no screens, no source, just default App.js.
react-native init MyApp
Install packages for which application asks via npm install.
I want to connect aws iot core like as mqtt client. But i don't know how config the tlsoptions, could anyone meet the same issue?
the source code is bellow:
` // MQTT 客户端选项
const options = {
clientId: 'mqtt-client-' + (Math.floor((Math.random() * 100000) + 1)),
reconnectPeriod: 30 * 1000,
qos: 2,
enableSsl: true,
tls:{
caDer: this.fetchCaDer(),
cert: this.fetchCert(),
key: this.fetchPrivateKey(),
}
};
const client = new Mqtt.Client("mqtt://xxx.iot.us-west-1.amazonaws.com:8883");
client.connect(options, err => {
console.log("GrillUtils connect err = " + err.toString())
console.log("GrillUtils connect err = " + JSON.stringify(err))
});
client.on(Mqtt.Event.Message, (topic, message) => {
console.log('Mqtt Message:', topic, message.toString());
console.log("GrillUtils message topic = " + topic + " data = " + data)
this.subscribeState = true
this.connected = true;
this.selfCheck(CheckStatus.FOM_HEART_BEAT);
if (data.value.hasOwnProperty("result")) {
this.convertResultTopic(data);
} else if (data.value.hasOwnProperty("heat")) {
this.convertHeartbeatTopic(data);
} else {
//don't care other heartbeat data.
this.convertSubscribeTopic(data)
}
});
client.on(Mqtt.Event.Connect, () => {
console.log('MQTT Connect');
console.log("GrillUtils connect ")
client.subscribe('heartbeat/' + this.grillID, (err) => {
if (err) {
console.log('Error subscribing to topic:', err);
} else {
console.log('Subscribed to topic');
}
});
// 订阅主题
client.subscribe('result/' + this.grillID)
client.subscribe('command/' + this.grillID)
});
client.on(Mqtt.Event.Error, (error: string) => {
console.log('MQTT Error:', error);
});
client.on(Mqtt.Event.Disconnect, (cause: string) => {
console.log('MQTT Disconnect:', cause);
});
///tlsoptions
fetchCaDer(){
const startLabel = "-----BEGIN CERTIFICATE-----";
const endLabel ="-----END CERTIFICATE-----"
return ${startLabel} MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF ... ${endLabel}
}
fetchCert(){
const startLabel = "-----BEGIN CERTIFICATE-----";
const endLabel ="-----END CERTIFICATE-----"
return ${startLabel} MIIDWjCCAkKgAwIBAgIVAKYgDNHgFHvnaJJhb9d0DmqOd0KwMA0GCSqGSIb3DQEB ... ${endLabel}
}
fetchPrivateKey(){
const startLabel = "-----BEGIN RSA PRIVATE KEY-----";
const endLabel ="-----END RSA PRIVATE KEY-----"
return ${startLabel} MIIEowIBAAKCAQEAtTA7FQ2Yopw3aQ3w0EBzQj0nmERQGW5FYRTsSlEBBijx9/ge ... Ew18DGPwHBLDhC9FrlHAcXsLOrESj6V+WCshz45nLbe2E+3T9MB0 ${endLabel}
}
`
I faced this problem , ho to fix it ?
can you update CocoaMqtt library so it can compile on xcode 13+
@davesters As per title, just wondering if there was insecure TLS support, and if so, how do we go about configuring it?
.../node_modules/react-native-native-mqtt/ios/Libraries/CocoaMQTT/CocoaMQTTTypes.swift:66:6: Enum cases with associated values cannot be marked potentially unavailable with '@available'
"(extension in Foundation):__C.NSURLSessionWebSocketTask.send(_: (extension in Foundation):__C.NSURLSessionWebSocketTask.Message, completionHandler: (Swift.Error?) -> ()) -> ()", referenced from:
react_native_native_mqtt.CocoaMQTTWebSocket.FoundationConnection.write(data: Foundation.Data, handler: (Swift.Error?) -> ()) -> () in libreact-native-native-mqtt.a(CocoaMQTTWebSocket.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
can I use when my device is Android is 4.1 or 4.4
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.