export default function App() {
MQTT.createClient({
uri: 'mqtt://test.mosquitto.org:1883',
clientId: 'id1',
//tls: false
}).then(function(client) {
client.on('closed', function() {
console.log('mqtt.event.closed');
});
client.on('error', function(msg) {
console.log('mqtt.event.error', msg);
});
client.on('message', function(msg) {
console.log('mqtt.event.message', msg);
});
client.on('connect', function() {
console.log('connected --------------------------->');
client.subscribe('/data', 0);
client.publish('/data', "test", 0, false);
});
client.connect();
}).catch(function(err){
console.log(err);
});
return (
<View style={styles.container}>
<Text>Hello World!!!!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
I imported MQTT this way:
import MQTT from 'sp-react-native-mqtt';
This library should be for react native and I copied the code from https://npmjs.com/package/sp-react-native-mqtt