Skip to content

Instantly share code, notes, and snippets.

@kwonghung-YIP
Created November 22, 2021 07:19
Show Gist options
  • Save kwonghung-YIP/199a5f8a3d7084bbc04308cdf25d2030 to your computer and use it in GitHub Desktop.
Save kwonghung-YIP/199a5f8a3d7084bbc04308cdf25d2030 to your computer and use it in GitHub Desktop.
Example App.js for including mqtt.js into local expo.dev project
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
// expo install mqtt
// fix refer to mqtt.js issue: https://github.com/mqttjs/MQTT.js/issues/573
const mqtt = require('mqtt/dist/mqtt')
export default function App() {
const brokerUrl = "...";
const username = "...";
const password = "...";
const client = mqtt.connect(brokerUrl,{
username: username,
password: password,
})
client.on('message',(topic,message,packet) => {
console.log(message.toString());
});
client.subscribe('test_topic');
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment