
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
@ko-developerhong/react-native-mqtt
Advanced tools
This library is used to communicate with the message broker using the MQTT protocol. This library is available in a React Native environment and supports TypeScript.
This library uses the following native MQTT client libraries:
To use this library, you must first install it using npm or yarn.
npm install @ko-developerhong/react-native-mqtt
# OR
yarn add @ko-developerhong/react-native-mqtt
Add Podfile
pod "CocoaMQTT", :modular_headers => true
pod "MqttCocoaAsyncSocket", :modular_headers => true
import MqttClient, { ConnectionOptions, ClientEvent, MQTTEventHandler } from 'react-native-mqtt';
To connect to the message broker, use the connect method, which takes the host address and connection options as arguments.
import MqttClient, { ConnectionOptions } from 'react-native-mqtt';
const options: ConnectionOptions = {
clientId: 'myClientId',
cleanSession: true,
keepAlive: 60,
timeout: 60,
maxInFlightMessages: 1,
autoReconnect: true,
username: 'myUsername',
password: 'myPassword',
tls: {
caDer: Buffer.from('myCertificate'),
cert: 'myCertificate',
key: 'myKey',
p12: Buffer.from('myP12'),
pass: 'myPass',
},
allowUntrustedCA: true,
enableSsl: true,
protocol: 'mqtts',
};
MqttClient.connect('mqtt://broker.hivemq.com', options)
.then(() => console.log('Connected'))
.catch((error) => console.error('Connection failed: ', error));
To subscribe to a specific topic, use the subscribe method.
type MQTT = {
subscribe: (topic: string, qos?: number) => void;
};
MqttClient.subscribe('myTopic', 0);
To publish a message, use the publish method.
type MQTT = {
publish: (topic: string, message: string, qos?: number , retained?: boolean) => void,
}
MqttClient.publish('myTopic', 'Hello, World!', 0, false);
This library offers a variety of events. To handle events, use the on, once, and off methods.
const onConnect: MQTTEventHandler<ClientEvent.Connect> = (reconnect) => {
console.log('Connected', reconnect);
};
MqttClient.on(ClientEvent.Connect, onConnect);
MqttClient.once(ClientEvent.Disconnect, (cause) => {
console.log('Disconnected', cause);
});
MqttClient.off(ClientEvent.Connect, onConnect);
To disconnect, use the disconnect method.
MqttClient.disconnect();
To close the library, use the close method.
MqttClient.close();
The Swift pod
CocoaMQTT
depends uponMqttCocoaAsyncSocket
, which does not define modules.
you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library
1.0.3
#5
FAQs
use mqtt in react-native
The npm package @ko-developerhong/react-native-mqtt receives a total of 17 weekly downloads. As such, @ko-developerhong/react-native-mqtt popularity was classified as not popular.
We found that @ko-developerhong/react-native-mqtt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.